一、表的构造
(list 风格)
days = { "Sunday" , "Monday" , "Tuesday", "Wednesday",
"Thursday" , "Friday" , "Saturday" }
Lua 将"Sunday" 初始化days[1](第一个元素索引为 1),用"Monday"初始化days[2]...
w = {x=0, y=0, label="console"}
x = {sin(0), sin(1), sin(2)}
w[1] = "another field"
x.f = w
print(w[ "x" ]) --> 0
print(w[1]) --> another field
print(x.f[1]) --> another field
w.x = nil -- remove field "x"
(record 风格)
opnames = {["+" ] = "add" , [ "-" ] = "sub" ,
["*" ] = "mul" , [ "/" ] = "div" }
i = 20; s = "-"
a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}
print(opnames[s]) --> sub
print(a[22]) --> ---
二、流程控制
(if)
if conditions then
then-part
elseif conditions then
elseif-part
else
else-part
end ;
(while)
while condition do
statements;
end ;
(repeat-until)
repeat
statements;
until conditions;
(数值for 循环)
for var=exp1,exp2,exp3 do
loop-part
end
(范型for 循环)
-- print all values of array 'a'
for i,v in ipairs(a) do print(v) end
-- print all keys of table 't'
for k in pairs(t) do print(k) end
范型for 和数值for 有两点相同:
1. 控制变量是局部变量
2. 不要修改控制变量的值
阅读(1053) | 评论(0) | 转发(0) |