我要做的是使用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
答案 0 :(得分:11)
要显示嵌套表,您必须使用嵌套循环。
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
答案 1 :(得分:6)
这递归地序列化一个表。此代码的变体可用于从表中生成JSON。
function tprint (tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
end
通过以下方式运行你的表:
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"
}
}
print (tprint(people))
生成这个:
{
[1] = {
name= "Fred",
phone= "123456",
address= "16 Long Street",
},
[2] = {
name= "Wilma",
phone= "123456",
address= "16 Long Street",
},
[3] = {
name= "Barney",
phone= "123457",
address= "17 Long Street",
},
}
答案 2 :(得分:4)
如果您的数据记录中有静态预定义字段名称,这个更简单的版本可能适合您:
let pdf = new pdfkit();
let buffers = [];
pdf.on('data', buffers.push.bind(buffers));
pdf.on('end', () => {
let pdfData = Buffer.concat(buffers);
// ... now send pdfData as attachment ...
});
pdf.text('Hello', 100, 100);
pdf.end();
答案 3 :(得分:0)
我不确定您要使用哪种IDE。但是由于任何原因,无论您是谁还是其他任何发现此线程的人都在Visual Studio Code中工作,Lua Debug extension将在为您构建的自定义表显示所有关联数组值方面做得很好。
我真正喜欢的是,不仅可以显示初始值,而且如果以后决定更改值,则可以使用此扩展名进行操作,并通过“调试控制台”查看所做的调整。标签。
我以您的确切示例为例,只需在调试中键入人员,然后显示所有值即可。
答案 4 :(得分:0)
解决方案1:py.repr
https://github.com/waketzheng/luapy
$ wget https://raw.githubusercontent.com/waketzheng/luapy/main/python.lua
py=require('python')
> tab = { 1, 2, 3 }
> py.repr(tab)
[
1,
2,
3
]
> tab = { a=1, b=2, c=3 }
> py.repr(tab)
{
"c": 3,
"a": 1,
"b": 2
}
> tab = { a='a', b='b', c='c', d='d', e='e', f='f', g='g' }
> py.repr(tab)
{
"g": "g",
"a": "a",
"b": "b",
"c": "c",
"d": "d",
...
}
解决方案2:lu.prettystr
https://luaunit.readthedocs.io/en/latest/#pretty-printing
$ wget https://raw.githubusercontent.com/bluebird75/luaunit/main/luaunit.lua
> lu = require('luaunit')
> t1 = {1,2,3}
> t1['toto'] = 'titi'
> t1.f = function () end
> t1.fa = (1 == 0)
> t1.tr = (1 == 1)
> print( lu.prettystr(t1) )
{1, 2, 3, f=function: 00635d68, fa=false, toto="titi", tr=true}
答案 5 :(得分:0)
假设您的数据结构是JSON可序列化的(如上述示例),您可以作弊并使用rxi/json.lua(MIT许可证)来帮助漂亮地打印对象。只需将json.lua放到您的项目中,即可使用:
json = require "json"
for k, v in pairs(people) do
print(k, json.encode(v))
end
1 {"address":"16 Long Street","name":"Fred","phone":"123456"}
2 {"address":"16 Long Street","name":"Wilma","phone":"123456"}
3 {"address":"17 Long Street","name":"Barney","phone":"123457"}