Lua表for循环,“for each x in ...”

时间:2013-07-20 00:50:16

标签: for-loop lua lua-table

blocks = {block1 = {}, block2 = {}}

blocks.block1.x = 1
blocks.block1.y = 2

blocks.block2.x = 3
blocks.block2.y = 4

我想为块表中的每个项目打印xy,例如

for b in blocks do
    print(b.x)
    print(b.y)
end 

我知道块最终会成为局部变量,但我不知道其他任何解释方法。

基本上我想要输出

>
1
2
3
4
> 

1 个答案:

答案 0 :(得分:2)

我不是专家,但你可以尝试一下吗?

for _, v in pairs(blocks) do
    print(v.x)
    print(v.y)
end

Output

1
2
3
4