全局变量:
print(b);--nil
b=10;
删除全局变量: b=nil;
lua中有8种基本数据类型:
nil(空),boolean(布尔),number(数字),string(字符串),userdata(自定义类型),function(函数),thread(线程),table(表)
lua将nil和false视为假,其余全为真
..是字符串连接操作
print(tostring(10)=="10") -->true
print(10 .. "" == "10") -->true
a="hello"
print(#a) -->5获取字符串的长度
table可以动态的添加元素
a={x=10,y=20,30}
a["z"]=40;
print(a["x"])-->10
print(a.x)-->10
print(a[1])-->30
a={{x=0,y=0}};
print(a[1].x);-->0
a={[1]="hello"};
print(a[1]); -->“hello”
逻辑运算符
and print(4 and 5) 第一个操作数为false,返回第一个操作数
or print(4 or 5) 第一个操作数为true,返回第一个操作数
局部变量
local i=1
a,b,c=0;
print(a,b,c);
严格控制某些局部变量的作用域
do
...
end
while i<=x do
....
end
至少会执行一次
repeat
line = io.read()
until line ~=""
for var=exp1,exp2,exp3 do
<执行体>
end
循环从exp1到exp2,步长位exp3,exp3如果没有,默认为1
if op=="+" then
...
elseif op=="-" then
...
else
...
end
栈
最下面的为1,最上面的为-1