gpt4 book ai didi

lua - 在lua中显示表格的内容

转载 作者:行者123 更新时间:2023-12-04 02:49:32 27 4
gpt4 key购买 nike

我想要做的是在 Lua 中使用以下代码显示表格的内容。

local people = {
{
name = "Fred",
address = "16 Long Street",
phone = "123456"
},

{
name = "Wilma",
address = "16 Long Street",
phone = "123456"
},

{
name = "Barney",
address = "17 Long Street",
phone = "123457"
}

}
for k, v in pairs(people ) do
print(k, v)
end

我得到的输出是:
1   table: 0x9a2d8b0
2 table: 0x9a2d110
3 table: 0x9a2cb28

最佳答案

要显示嵌套表,您必须使用嵌套循环。

此外,使用 ipairs 遍历类数组表,使用 pairs 遍历类记录表。

local people = {
{
name = "Fred",
address = "16 Long Street",
phone = "123456"
},
{
name = "Wilma",
address = "16 Long Street",
phone = "123456"
},
{
name = "Barney",
address = "17 Long Street",
phone = "123457"
}
}

for index, data in ipairs(people) do
print(index)

for key, value in pairs(data) do
print('\t', key, value)
end
end

输出:
1   
phone 123456
name Fred
address 16 Long Street
2
phone 123456
name Wilma
address 16 Long Street
3
phone 123457
name Barney
address 17 Long Street

关于lua - 在lua中显示表格的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41942289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com