首页 > 解决方案 > 当嵌套表与其他数据类型混合时,如何循环遍历 Lua 中的嵌套表?

问题描述

我正在尝试通过 Lua 中的一个非常大的表进行循环,该表由许多嵌套表的混合数据类型组成。我想将整个数据表打印到控制台,但我遇到了嵌套循环的问题。当我执行嵌套循环以打印下一级深度键值对时,我收到此错误bad argument #1 to 'pairs' (table expected, got number),因为并非所有值都是表。

我试过if type(value) == table then在嵌套循环之前添加一个,但它永远不会触发,因为type(value)返回userdata它们是整数、字符串还是表。
编辑:我错了,只有表作为类型返回userdata

我的表看起来像这样,但有数百对,可以是几个嵌套表。我为此使用的工具有一个很棒的内置方法printall(),但它只适用于第一个嵌套表。我无法控制这张表的外观,我只是在玩游戏的数据,不胜感激。

myTable = {
    key1 = { value1 = "string" },
    key2 = int,
    key3 = {             -- printall() will print all these two as key value pairs
        subKey1 = int, 
        subKey2 = int
    },
    key4 = {
        innerKey1 = { -- printall() returns something like : innerKey1 = <int32_t[]: 0x13e9dcb98>
            nestedValue1 = "string",
            nestedValue2 = "string"
        },
        innerKey2 = { -- printall() returns something like : innerKey2 = <vector<int32_t>[41]: 0x13e9dcbc8>
            nestedValue3 = int,
            nestedValue4 = int
        }
    },
    keyN = "string"
}

我的循环

for key, value in pairs(myTable) do
    print(key)
    printall(value)
    for k,v in pairs(value) do
            print(k)
            printall(v)
        end
    end
    print("====")
end

回答:这是我修复此问题的函数的最终版本,它对 Nifim 给出的答案稍作修改,以捕捉破坏它的边缘情况。

function printFullObjectTree(t, tabs)
    local nesting = ""
    for i = 0, tabs, 1 do
        nesting = nesting .. "\t"
    end
    for k, v in pairs(t) do
        if type(v) == "userdata" then     -- all tables in this object are the type `userdata` 
            print(nesting .. k .. " = {")
            printFullObjectTree(v, tabs + 1)
            print(nesting .. "}")
        elseif v == nil then
            print(nesting .. k .. " = nil")
        elseif type(v) == "boolean" then
            print(nesting .. k .. " = " .. string.format("%s", v))
        else
            print(nesting .. k .. " = " .. v)
        end
    end
end

标签: luanested-loopslua-table

解决方案


type(value)返回一个表示值类型的字符串

更多信息在这里: lua-users.org/wiki/TypeIntrospection

此外,您的示例表具有int某些键的一些值,因为对于下面的示例,这些键基本上不是表的一部分,因此我会将每个实例更改int为数字值。

如果您点击表格而不是制作未知数量的嵌套循环,那么递归也是有意义的。

这是一个工作的例子printAll

myTable = {
    key1 = { value1 = "string" },
    key2 = 2,
    key3 = {             -- printall() will print all these two as key value pairs
        subKey1 = 1, 
        subKey2 = 2
    },
    key4 = {
        innerKey1 = { -- printall() returns something like : innerKey1 = <int32_t[]: 0x13e9dcb98>
            nestedValue1 = "string",
            nestedValue2 = "string"
        },
        innerKey2 = { -- printall() returns something like : innerKey2 = <vector<int32_t>[41]: 0x13e9dcbc8>
            nestedValue3 = 3,
            nestedValue4 = 4
        }
    },
    keyN = "string"
}

function printAll(t, tabs)
    local nesting = ""
    for i = 0, tabs, 1 do
        nesting = nesting .. "\t"
    end
    for k, v in pairs(t) do
        if type(v) == "table" then
            print(nesting .. k .. " = {")
            printAll(v, tabs + 1)
            print(nesting .. "}")
        else
            print(nesting .. k .. " = " .. v)
        end
    end
end

print("myTable = {")
printAll(myTable, 0)
print("}")

推荐阅读