显示 lua 中表格的内容
Display contents of tables in lua
我想做的是在 Lua 中使用以下代码显示 table 的内容。
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
to iterate through array-like tables, and 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
这递归地序列化一个table。此代码的变体可用于从 table.
生成 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
运行 你的 table 通过这个:
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",
},
}
如果您的数据记录中有静态预定义字段名称,这个更简单的版本可能适合您:
for i,t in ipairs(people) do
print('Record',i)
print('Name',t.name)
print('Address',t.address)
print('Phone',t.phone)
print()
end
我不确定 IDE 你在锻炼什么。但是无论出于何种原因,您和其他发现此线程的人都在使用 Visual Studio 代码,Lua Debug extension 将在显示您构建的自定义表的所有关联数组值方面做得很好。
我真正喜欢它的地方在于,您不仅可以显示您的初始值,而且如果您决定稍后更改值,您可以使用此扩展程序执行此操作并查看您的调整,整个过程 "Debug Console" 选项卡。
我以你的例子为例,简单地在调试中输入 people,然后显示所有值。
解决方案 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}
假设您的数据结构是 JSON 可序列化的(如您上面的示例),您可以欺骗并在您的项目中使用 rxi/json.lua (MIT License) to aid in pretty printing objects. Just drop 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"}
我想做的是在 Lua 中使用以下代码显示 table 的内容。
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
to iterate through array-like tables, and 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
这递归地序列化一个table。此代码的变体可用于从 table.
生成 JSONfunction 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
运行 你的 table 通过这个:
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",
},
}
如果您的数据记录中有静态预定义字段名称,这个更简单的版本可能适合您:
for i,t in ipairs(people) do
print('Record',i)
print('Name',t.name)
print('Address',t.address)
print('Phone',t.phone)
print()
end
我不确定 IDE 你在锻炼什么。但是无论出于何种原因,您和其他发现此线程的人都在使用 Visual Studio 代码,Lua Debug extension 将在显示您构建的自定义表的所有关联数组值方面做得很好。
我真正喜欢它的地方在于,您不仅可以显示您的初始值,而且如果您决定稍后更改值,您可以使用此扩展程序执行此操作并查看您的调整,整个过程 "Debug Console" 选项卡。
我以你的例子为例,简单地在调试中输入 people,然后显示所有值。
解决方案 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}
假设您的数据结构是 JSON 可序列化的(如您上面的示例),您可以欺骗并在您的项目中使用 rxi/json.lua (MIT License) to aid in pretty printing objects. Just drop 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"}