在lua中显示表的内容

Nee*_*enu 9 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 Street",
   phone = "123457"
   }

}
for k, v in pairs(people ) do
    print(k, v)
end
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

1   table: 0x9a2d8b0
2   table: 0x9a2d110
3   table: 0x9a2cb28
Run Code Online (Sandbox Code Playgroud)

Oka*_*Oka 12

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

此外,用于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
Run Code Online (Sandbox Code Playgroud)

输出:

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  
Run Code Online (Sandbox Code Playgroud)

  • @Oka 如何动态判断表是类数组还是类记录? (2认同)

Lui*_*zes 8

这递归地序列化了一个表.此代码的变体可用于从表生成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
Run Code Online (Sandbox Code Playgroud)

通过这个运行你的表:

 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))
Run Code Online (Sandbox Code Playgroud)

生成这个:

  {
  [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",
    },
}
Run Code Online (Sandbox Code Playgroud)


ton*_*mtr 6

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

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

  • 这样做的优点是您可以选择字段的显示顺序。 (3认同)

m42*_*m42 5

如果您需要在Neovim代码中转储表内容,可以使用此方法,该方法内置于标准库中。

print( vim.inspect(table) )
Run Code Online (Sandbox Code Playgroud)

来自 Neovim 文档:

Lua标准模块 lua-stdlib

Nvim Lua“标准库”(stdlib)是vim模块,它公开了各种函数和子模块。它总是被加载的,因此 require("vim") 是不必要的。您可以查看模块属性: :lua print(vim.inspect(vim)) 结果如下:

{
  _os_proc_children = <function 1>,
  _os_proc_info = <function 2>,
  ...
  api = {
    nvim__id = <function 5>,
    nvim__id_array = <function 6>,
    ...
  },
  deepcopy = <function 106>,
  gsplit = <function 107>,
  ...
}
Run Code Online (Sandbox Code Playgroud)