Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display contents of tables in lua

Tags:

lua

lua-table

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
like image 899
Neenu Avatar asked Jan 30 '17 17:01

Neenu


People also ask

How do you read tables in Lua?

local Table = { "value_1"; "value_2"; "value_3"; "value_4"; }; for Key = 1, #Table, 1 do print(Table[Key]); end; Feel free to ask any questions. Oh, and if you're planning on running this code many times, consider putting local print = print; above your code to define a local variable (they are faster).

Does Lua have list?

The Linked lists in Lua are of two kinds, namely singly-linked lists and doubly linked list. A singly linked list in Lua will have a reference pointing to the first node in a singly linked list, and there is a reference from each node to the next node in a singly linked list.

How do I print a value in Lua?

local message = "Hello, this is a message!" -- Made a smaller version of the string, so you don't have to copy and paste/type it out every time.. print(message) -- Prints a message into the console, which in this case is the variable!


2 Answers

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  
like image 123
Oka Avatar answered Sep 17 '22 12:09

Oka


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",
    },
}
like image 29
Luiz Menezes Avatar answered Sep 19 '22 12:09

Luiz Menezes