Neenu
Neenu

Reputation: 529

Display contents of tables in lua

What I'm trying to do is display the content of table using the following code in 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

The output I got is:

1   table: 0x9a2d8b0
2   table: 0x9a2d110
3   table: 0x9a2cb28

Upvotes: 28

Views: 89805

Answers (8)

Ahmed
Ahmed

Reputation: 396

We can also ignore index:

for _, person in pairs(people) do
    for k,v in pairs(person) do
        print(k,v)
    end
end

Upvotes: 0

OldBuildingAndLoan
OldBuildingAndLoan

Reputation: 3032

If you need to dump table contents within Neovim code, you can use this method, which is built into the standard lib.

print( vim.inspect(table) )

From the Neovim documentation:

Lua standard modules lua-stdlib

The Nvim Lua "standard library" (stdlib) is the vim module, which exposes various functions and sub-modules. It is always loaded, thus require("vim") is unnecessary. You can peek at the module properties: :lua print(vim.inspect(vim)) Result is something like this:

{
  _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>,
  ...
}

Upvotes: 16

notpeter
notpeter

Reputation: 1130

Assuming your data structures are JSON serializable (like your example above) you can just cheat and use rxi/json.lua (MIT License) to aid in pretty printing objects. Just drop json.lua into your project and this will work:

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"}

Upvotes: 2

Waket Zheng
Waket Zheng

Reputation: 6371

Solution 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",
    ...
}

Solution 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}

Upvotes: 2

klewis
klewis

Reputation: 8379

I'm not sure what IDE you are working out of. But for any reason you and anyone else who finds this thread, is working in Visual Studio Code, the Lua Debug extension will do a great job in displaying all your associative array values for the custom tables you build.

What I really like about it, is that you can display not only your initial values, but if you decide to later change a value, you can do that with this extension and see your adjustments, all through the "Debug Console" tab.

I took your exact example, and simply typed in people into the debug, and go all values displayed.

Upvotes: 0

tonypdmtr
tonypdmtr

Reputation: 3235

If you have static predefined field names in your data records, this simpler version may work for you:

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

Upvotes: 7

Luiz Menezes
Luiz Menezes

Reputation: 819

This recursively serializes a table. A variant of this code may be used to generate JSON from a table.

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

running your table through this:

 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))

generates this:

  {
  [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",
    },
}

Upvotes: 35

Oka
Oka

Reputation: 26375

To display nested tables you will have to use nested loops.

Also, use ipairs to iterate through array-like tables, and pairs to iterate through record-like tables.

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

Output:

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  

Upvotes: 23

Related Questions