Search code examples
lua

Lua- Iterating nested table


I have been learning Lua for some weeks now and this is my one sticking point time and time again. I have tried to read posts and books on this topic.

I use Lua to query a software monitoring system (Nimsoft) and my data is returned to me in a table.

I will not post the whole output but here is a snippet I think will describe the structure:

The table referance is "h_resp"

root:
      domain:nevil-nmsdom
      robotlist:
        1:
          ssl_mode:0
          os_user2:
          origin:nevil-nmshub
          os_major:UNIX
          ip:192.168.1.58
          os_minor:Linux
          addr:/nevil-nmsdom/nevil-nmshub/nevil-multibot_03
          status:0
          license:1
          last_inst_change:1340754931
          created:1341306789
          offline:0
          last_change:1341306869
          lastupdate:1344522976
          autoremove:0
          os_user1:
          flags:1
          os_description:Linux 2.6.32-5-amd64 #1 SMP Mon Jan 16 16:22:28 UTC 2012 x86_64
          name:nevil-multibot_03
          metric_id:M64FB142FE77606C2E924DD91FFCC3BB4
          device_id:DDFF83AB8CD8BC99B88221524F9320D22
          heartbeat:900
          port:48100
          version:5.52 Dec 29 2011
        2: etc...etc....

I use a tdump function I found on this forum to achieve this.

for k,v in pairs(h_resp) do
print(k.."    ",v)
end

Gives me the top level, I understand this.

domain    nevil-nmsdom
robotlist    table:0x22136a0

Then I try to get the "robotlist"

for k,v in pairs(h_resp.robotlist) do
print(k.."    ",v)
end

As you can see below the indexes are integers and vales another table.

  1    table:0x237e530
  0    table:0x22112a0
  3    table:0x2211460
  2    table:0x2392ee0
  5    table:0x2213e80
  4    table:0x22130e0
  7    table:0x2283b80
  6    table:0x2283ff0
  8    table:0x22a71e0

I also get the fact I can address ONE of these "nested" tables using:

for k,v in pairs(h_resp.robotlist["0"]) do
print(k.."    ",v)
end



  ssl_mode    0
  os_user2    
  origin    network
  os_major    UNIX
  ip    192.168.1.31
  os_minor    Linux
  addr    /nevil-nmsdom/nevil-nmshub/nevil-mysql
  status    0
  ...etc...etc...

To my point, I cannot work out how to ask Lua to iterate over ALL the tables stored in robotlist.

Second I appologise for the long winded email but i am still trying to learn/make sense of this.... I have no previous programming/scripting experiance.

Thanks


Solution

  • If you want to print the table list, and then the insides of every table, and then again (much like in inception), the easiest way is to use recursion.

    You will need to check the type of the current element of the table you are looking at:

    function DeepPrint (e)
        -- if e is a table, we should iterate over its elements
        if type(e) == "table" then
            for k,v in pairs(e) do -- for every element in the table
                print(k)
                DeepPrint(v)       -- recursively repeat the same procedure
            end
        else -- if not, we can just print it
            print(e)
        end
    end
    

    You should look at Lua manual, everything is explained there. //EDIT: I should be more clear; there's a section in the manual containing the function very similar to the above.