Lua中显示表格内容

我在尝试使用以下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
点赞
用户2505965
用户2505965

要显示嵌套的表格,您将需要使用嵌套循环。

此外,使用 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
2017-01-30 18:25:27
用户7066323
用户7066323

这个函数可以递归地序列化一个表。可以使用此代码的一种变体从表格生成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",
    },
}
2017-01-30 18:54:08
用户3735873
用户3735873

如果您的数据记录具有静态预定义的字段名称,则此更简单的版本可能适合您使用:

for i,t in ipairs(people) do
  print('记录',i)
  print('姓名',t.name)
  print('地址',t.address)
  print('电话',t.phone)
  print()
end
2017-01-31 00:08:54
用户957186
用户957186

我不确定你正在使用哪个IDE。但是无论出于何种原因,如果你和其他在使用Visual Studio Code的人们遇到相同的情况,那么Lua Debug extension将会非常好地展示你建立的自定义表中所有的键值对。

我喜欢这个扩展的原因,是你不仅可以显示你的初始值,而且如果你决定稍后更改一个值,你也可以通过这个扩展来进行更改,并通过“Debug Console”选项卡查看你的调整。

我使用了你的精确示例,只需在debug中键入"people",所有的值都可以显示出来。

2020-01-05 04:58:09
用户9586338
用户9586338

解决方案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}
2020-10-17 13:10:15
用户93534
用户93534

假設你的資料結構可被 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"}
2020-11-25 01:01:02