如何在表格中打印表格的内容? [卢阿]

问题描述 投票:1回答:1

我只想在表中打印表的内容。例如:

local stats = {
  table1 = {
    tTable1 = 
    {
      data = 1
    },
    tTable2 = 
    {
      data2 = 2
    },
    tTable3 =
    {
      data3 = 3
    },
  }
}

我并不真正在乎table1或所有tTable,而是在数据变量中的信息。如何打印?

这是我的真实代码的片段:

local stats = {
  [1] = { 
    [1] = { 
      [1] = 1,
      [2] = -1,
      [3] = -1,
      ["n"] = 3,
    },
    [2] = { 
      [1] = nuclearcraft:cooler,
      [2] = 10,
      ["n"] = 2,
    },
    ["n"] = 2,
  },
  [2] = { 
    [1] = {
      [1] = 2,
      [2] = -1,
      [3] = -1,
      ["n"] = 3,
    },
    [2] = { 
      [1] = nuclearcraft:cell_block,
      [2] = 0,
      ["n"] = 2,
    },
    ["n"] = 2,
  },
  [3] = {
    [1] = {
      [1] = 3,
      [2] = -1,
      [3] = -1,
      ["n"] = 3,
    },
    [2] = { 
      [1] = nuclearcraft:cooler,
      [2] = 10,
      ["n"] = 2,
    },
    ["n"] = 2,
  },
}

此代码实际上持续的时间比这更长。在真实的代码中,我不关心任何数据,除了上面写着“ nuclearcraft”的区域及其下方的数字。

lua lua-table
1个回答
0
投票

递归表遍历适用于这种情况:

local function TablePrint(t)
     for k,v in pairs(t)  do
         if type(v)=="table" then
            print(k)
            TablePrint(v)
         else 
            print('\t',k,v)
         end
     end      
end
TablePrint(stats)

结果:

table1
tTable3
        data3   3
tTable2
        data2   2
tTable1
        data    1

请记住,未定义表中非索引值的顺序

© www.soinside.com 2019 - 2024. All rights reserved.