Lua-迭代嵌套表 [英] Lua- Iterating nested table

查看:69
本文介绍了Lua-迭代嵌套表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我使用Lua查询软件监视系统(Nimsoft),并且我的数据以表格的形式返回给我.

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:

表引用为"h_resp"

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

我使用在该论坛上找到的tdump函数来实现这一目标.

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

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

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.

谢谢

推荐答案

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

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

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

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

这篇关于Lua-迭代嵌套表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆