SpringDragon

导航

 

#取得tabel(键值对)表中的最大索引,不是tabel的个数
Lua中的数组:
array={"lua","C#"}
1、索引默认从1开始的
2、索引可以为负数的(表为键值对的形式)
array = {};
for i=-2,2 do
  array[i] = i*3;
end

for i=-2,2 do
  print(array[i])
end
Lua中的多维数组:
array = {{"a","A"},{"b","B"},{"c","C"}}
for i=1,#array do
  for j=1,#array[i] do
    print(array[i][j])
  end
end

注:pairs:遍历表中所有的key和value
ipairs:遍历数组,按照索引从1开始递增遍历,遇到nil值停止
迭代函数是可以自定义的
--迭代函数会有返回值,返回值会赋值给变量列表
for 变量列表 in 迭代函数,状态变量,控制变量 do
  --循环体
end

function square(state,control)
  if(control>=state) then
    return nil;
  else
    control = control+1;
  return control,control*control;
  end
end

for i,j in square,9,0 do
  print(i,j);
end

posted on 2019-05-28 11:33  chenquanlong  阅读(215)  评论(0编辑  收藏  举报