在lua中显示表的内容

在lua中显示表的内容,lua,lua-table,Lua,Lua Table,我要做的是在Lua中使用以下代码显示表的内容 local people = { { name = "Fred", address = "16 Long Street", phone = "123456" }, { name = "Wilma", address = "16 Long Street", phone = "123456" }, { name = "Barney", address = "17 Long

我要做的是在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

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

另外,用于迭代类似于数组的表,以及迭代类似于记录的表

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  

这将递归地序列化一个表。此代码的变体可用于从表生成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",
    },
}

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

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代码中工作,将在显示您构建的自定义表的所有关联数组值方面做得非常好

我真正喜欢的是,您不仅可以显示初始值,而且如果您决定以后更改值,您可以使用此扩展来执行此操作,并通过“调试控制台”选项卡查看您的调整


我以您的例子为例,只需在调试中输入人员,然后查看所有显示的值。

解决方案1:
py.repr

$wgethttps://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",
    ...
}
> 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}
解决方案2:
lu.prettystr

$wgethttps://raw.githubusercontent.com/bluebird75/luaunit/main/luaunit.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",
    ...
}
> 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可序列化的(如上面的示例),您可以欺骗并使用(MIT许可证)来帮助打印漂亮的对象。只要投入到您的项目中,这将起作用:

json=需要“json”
对于k,v成对(人)做
打印(k,json.encode(v))
结束

这样做的好处是,您可以选择字段显示的顺序。非常感谢。这真是太棒了helpful@Oka如何动态地判断表是类似于数组还是类似于记录?