赞
踩
1
2
3
for i = 1,10,1 do --初值,终值,步数(正数则每次循环加该数,负数则i减该数),步数可以省略(默认为1)
for
i =
,
10
do
--初值,终值,步数(正数则每次循环加该数,负数则i减该数),步数可以省略(默认为
)
print(i*2); --2,4,6,...20
print(i*
); --
4
6
,...
20
end
这种遍历表的方式会遍历所有的键值对
5
days = {"Sunday", x = 1, y = 2,"Monday", "Tuesday", "Wednesday",
days = {
"Sunday"
, x =
, y =
"Monday"
"Tuesday"
"Wednesday"
"Thursday", "Friday", [20] = "aaa","Saturday"}
"Thursday"
"Friday"
, [
] =
"aaa"
"Saturday"
}
for k,v in pairs(days) do
k,v in pairs(days)
print(k,v)//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday},{x,1},{20,"aaa"}{y,2}
print(k,v)
//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday},{x,1},{20,"aaa"}{y,2}
这种遍历表的方式会从数字索引(interger key)1开始遍历,如果下一个数字索引不存在,则结束
"Thursday", "Friday",[20] = "aaa", "Saturday"}
,[
for k,v in ipairs(days) do
k,v in ipairs(days)
print(k,v)//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday}
//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday}