Lua-迭代嵌套表


Lua- Iterating nested table

我已经学习Lua几周了,这是我一次又一次的症结所在。我尝试阅读有关此主题的文章和书籍。

我使用Lua来查询软件监视系统(Nimsoft),并且我的数据在表中返回给我。

我不会发布整个输出,但是以下是我认为将描述结构的代码段:

表参考是" h_resp"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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....

我使用在该论坛上找到的tdump函数来实现此目的。

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

给我最高层次,我理解这一点。

1
2
domain    nevil-nmsdom
robotlist    table:0x22136a0

然后,我尝试获取"机器人列表"

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

正如您在下面看到的,索引是整数,而值是另一个表。

1
2
3
4
5
6
7
8
9
  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

我还得到了一个事实,我可以使用以下地址来处理这些"嵌套"表之一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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...

就我而言,我无法弄清楚如何让Lua遍历robotlist中存储的所有表。

其次,我为冗长的电子邮件表示歉意,但我仍在尝试学习/理解这一点。...我以前没有编程/脚本编写经验。

谢谢


如果要打印表列表,然后再打印每个表的内部,然后再打印(很像在开始时一样),最简单的方法是使用递归。

您将需要检查正在查看的表的当前元素的类型:

1
2
3
4
5
6
7
8
9
10
11
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

您应该看一下Lua手册,那里有所有解释。
//编辑:我应该更清楚一些; 手册中有一部分包含与上述功能非常相似的功能。


@Bartek Banachewicz的回答对我很有帮助。 但是,我确实必须更改代码的顺序才能使其正常工作。 我的代码与下面的代码明显不同,但是使用与上面相同的思想对我来说很有效。

注意,我需要将if-else块移到迭代块内,并检查类型v。

1
2
3
4
5
6
7
8
9
10
11
function DeepPrint (e)
-- if e is a table, we should iterate over its elements
    for k,v in pairs(e) do -- for every element in the table
        if type(v) =="table" then
          print(k)
          DeepPrint(v)       -- recursively repeat the same procedure
        else -- if not, we can just print it
          //EDIT: print(v .. k)
        end
    end
end

谢谢你的指导