如何打印 Lua table 中的所有值?
How to print all values in a Lua table?
如果我有这样的 table,我将如何打印所有值?
local Buyers = {
{[Name] = "Birk", [SecName] = "Birk2nd", [ThirdName] = "Birk3nd"},
{[Name] = "Bob", [SecName] = "Bob2nd", [ThirdName] = "Bob3nd"},
}
它应该最终打印:
First Name: Birk
Second Name: Birk2nd
Third Name: Birk3nd
FirstName: Bob
Second Name: Bob2nd
Third Name: Bob3nd
对于你的情况,像这样:
local function serialise_buyer (buyer)
return ('First Name: ' .. (buyer.Name or '')) .. '\n'
.. ('Second Name: ' .. (buyer.SecName or '')) .. '\n'
.. ('Third Name: ' .. (buyer.ThirdName or '')) .. '\n'
end
local Buyers = {
{Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
{Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
}
for _, buyer in ipairs (Buyers) do
print (serialise_buyer (buyer))
end
更通用的解决方案,带排序:
local sort, rep, concat = table.sort, string.rep, table.concat
local function serialise (var, sorted, indent)
if type (var) == 'string' then
return "'" .. var .. "'"
elseif type (var) == 'table' then
local keys = {}
for key, _ in pairs (var) do
keys[#keys + 1] = key
end
if sorted then
sort (keys, function (a, b)
if type (a) == type (b) and (type (a) == 'number' or type (a) == 'string') then
return a < b
elseif type (a) == 'number' and type (b) ~= 'number' then
return true
else
return false
end
end)
end
local strings = {}
local indent = indent or 0
for _, key in ipairs (keys) do
strings [#strings + 1]
= rep ('\t', indent + 1)
.. serialise (key, sorted, indent + 1)
.. ' = '
.. serialise (var [key], sorted, indent + 1)
end
return 'table (\n' .. concat (strings, '\n') .. '\n' .. rep ('\t', indent) .. ')'
else
return tostring (var)
end
end
local Buyers = {
{Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
{Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
[function () end] = 'func',
[{'b', 'd'}] = {'e', 'f'}
}
print (serialise (Buyers, true))
我能想到的
local Buyers = {
{["Name"] = "Birk", ["SecName"] = "Birk2nd", ["ThirdName"] = "Birk3nd"},
{["Name"] = "Bob", ["SecName"] = "Bob2nd", ["ThirdName"] = "Bob3nd"},
}
for _, person in pairs(Buyers) do
print("First name: "..person.Name)
print("Second name: "..person.SecName)
print("Third name: "..person.ThirdName)
print()
end
如果我有这样的 table,我将如何打印所有值?
local Buyers = {
{[Name] = "Birk", [SecName] = "Birk2nd", [ThirdName] = "Birk3nd"},
{[Name] = "Bob", [SecName] = "Bob2nd", [ThirdName] = "Bob3nd"},
}
它应该最终打印:
First Name: Birk
Second Name: Birk2nd
Third Name: Birk3nd
FirstName: Bob
Second Name: Bob2nd
Third Name: Bob3nd
对于你的情况,像这样:
local function serialise_buyer (buyer)
return ('First Name: ' .. (buyer.Name or '')) .. '\n'
.. ('Second Name: ' .. (buyer.SecName or '')) .. '\n'
.. ('Third Name: ' .. (buyer.ThirdName or '')) .. '\n'
end
local Buyers = {
{Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
{Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
}
for _, buyer in ipairs (Buyers) do
print (serialise_buyer (buyer))
end
更通用的解决方案,带排序:
local sort, rep, concat = table.sort, string.rep, table.concat
local function serialise (var, sorted, indent)
if type (var) == 'string' then
return "'" .. var .. "'"
elseif type (var) == 'table' then
local keys = {}
for key, _ in pairs (var) do
keys[#keys + 1] = key
end
if sorted then
sort (keys, function (a, b)
if type (a) == type (b) and (type (a) == 'number' or type (a) == 'string') then
return a < b
elseif type (a) == 'number' and type (b) ~= 'number' then
return true
else
return false
end
end)
end
local strings = {}
local indent = indent or 0
for _, key in ipairs (keys) do
strings [#strings + 1]
= rep ('\t', indent + 1)
.. serialise (key, sorted, indent + 1)
.. ' = '
.. serialise (var [key], sorted, indent + 1)
end
return 'table (\n' .. concat (strings, '\n') .. '\n' .. rep ('\t', indent) .. ')'
else
return tostring (var)
end
end
local Buyers = {
{Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
{Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
[function () end] = 'func',
[{'b', 'd'}] = {'e', 'f'}
}
print (serialise (Buyers, true))
我能想到的
local Buyers = {
{["Name"] = "Birk", ["SecName"] = "Birk2nd", ["ThirdName"] = "Birk3nd"},
{["Name"] = "Bob", ["SecName"] = "Bob2nd", ["ThirdName"] = "Bob3nd"},
}
for _, person in pairs(Buyers) do
print("First name: "..person.Name)
print("Second name: "..person.SecName)
print("Third name: "..person.ThirdName)
print()
end