我有以下 lua 表:
t = {name="sandy", age="22", major="computerScience"}
我想迭代它,下面的两种方法都不起作用
方法一
function printAssociateTable(t) print("before the loop") for i,v in ipairs(t) do print(v) end end
方法二
function printAssociateTable2(t) print("before the loop") for k in ipairs(t) do print("before the loop") print(k.. " is " ..tk) end end
当我调用任何一种方法时,它们都会打印“循环前”。所以问题是程序没有进入循环!
我试图手动访问表,如果我这样做:
return t.name => 给桑迪
return t[name] => 给出nil
return t["name"] => 给了桑迪!
我怎样才能遍历我的表?请注意,这两种方法都适用于像这样的简单数组
a={"a","b","c","d"}
但我希望它在我的关联数组上工作