当嵌套表与其他数据类型混合时,如何在Lua中循环嵌套表?

当嵌套表与其他数据类型混合时,如何在Lua中循环嵌套表?,lua,nested-loops,lua-table,Lua,Nested Loops,Lua Table,我尝试在Lua中循环一个非常大的表,它由混合数据类型和许多嵌套表组成。我想将整个数据表打印到控制台,但嵌套循环有问题。当我执行嵌套循环以打印下一级深键值对时,我会得到以下错误错误参数#1到'pairs'(预期为表,获得了数字),因为并非所有值都是表 我尝试在嵌套循环之前添加if-type(value)==table-then,但它从未触发,因为type(value)返回userdata,无论它们是int、string还是table。 编辑:我错了,只有表作为typeuserdata返回 我的表看

我尝试在Lua中循环一个非常大的表,它由混合数据类型和许多嵌套表组成。我想将整个数据表打印到控制台,但嵌套循环有问题。当我执行嵌套循环以打印下一级深键值对时,我会得到以下错误
错误参数#1到'pairs'(预期为表,获得了数字)
,因为并非所有值都是表

我尝试在嵌套循环之前添加
if-type(value)==table-then
,但它从未触发,因为
type(value)
返回
userdata
,无论它们是int、string还是table。
编辑:我错了,只有表作为type
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"
}
回答:这是我最后一个版本的函数,它修正了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
type(value)
返回表示值类型的字符串

有关这方面的更多信息,请参见:

此外,您的示例表将
int
作为某些键的一些值,因为这将是nil,这些键基本上不是我下面的示例表的一部分。我将把
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("}")
myTable={
key1={value1=“string”},
键2=2,
key3={--printall()将把这两个都打印为键值对
子键1=1,
子键2=2
},
键4={
innerKey1={--printall()返回类似于:innerKey1=
nestedValue1=“字符串”,
nestedValue2=“字符串”
},
innerKey2={--printall()返回类似于:innerKey2=
nestedValue3=3,
nestedValue4=4
}
},
keyN=“字符串”
}
函数printAll(t,tabs)
本地嵌套=“”
对于i=0,制表符为1
嵌套=嵌套。。“\t”
结束
对于k,v成对(t)do
如果类型(v)=“表”,则
打印(嵌套..k..“={”)
全部打印(v,制表符+1)
打印(嵌套..“}”)
其他的
打印(嵌套..k..“=”.v)
结束
结束
结束
打印(“myTable={”)
printAll(myTable,0)
打印(“}”)

感谢您的回复。我不得不做一些修改来处理边缘案例,但我做得很好,我会将最终版本添加到我的帖子中,因为这篇评论太长了。我相信它现在正在捕捉每一个值和表。
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("}")