【问题标题】:Display contents of tables in lua在lua中显示表的内容
【发布时间】:2017-06-15 23:41:22
【问题描述】:

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

【问题讨论】:

    标签: lua lua-table


    【解决方案1】:

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

    另外,使用ipairs 遍历array 类表,使用pairs 遍历record 类表。

    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  
    

    【讨论】:

    • @Oka 如何动态判断表是类数组还是类记录?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果您的数据记录中有静态的预定义字段名称,这个更简单的版本可能适合您:

      for i,t in ipairs(people) do
        print('Record',i)
        print('Name',t.name)
        print('Address',t.address)
        print('Phone',t.phone)
        print()
      end
      

      【讨论】:

      • 这样的好处是你可以选择字段的显示顺序。
      【解决方案4】:

      解决方案一:py.reprhttps://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.prettystrhttps://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】:

        我不确定您使用的是什么 IDE。但无论出于何种原因,您和其他任何发现此线程的人都在 Visual Studio Code 中工作,Lua Debug extension 将在显示您构建的自定义表的所有关联数组值方面做得很好。

        我真正喜欢它的是,您不仅可以显示初始值,而且如果您决定稍后更改值,您可以使用此扩展程序执行此操作并查看您的调整,所有这些都通过“调试控制台”标签。

        我以您的确切示例为例,只是在调试中输入人员,然后显示所有值。

        【讨论】:

          【解决方案6】:

          假设您的数据结构是 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"}
          

          【讨论】:

            猜你喜欢
            • 2022-11-01
            • 2017-07-16
            • 1970-01-01
            • 2013-10-06
            • 2013-07-13
            • 2015-01-12
            • 1970-01-01
            • 1970-01-01
            • 2020-11-17
            相关资源
            最近更新 更多