问题:用Lua对i、v的数值进行制表符对齐打印

我解释一下我实际上做了一个 trrtable 函数

工作原理如下:

local function treeTable(obj)
    if type(obj) == "table" then
        for i, v in pairs(obj) do
            print(tostring(i) .. "\t-> <" .. type(v) .. "> " .. tostring(v))
        end
    else
        print("不允许 '" .. type(obj) .. "' 值。")
    end
end

local table = {
    Number = 1,
    String = "Hello world",
    Boolean = true,
    Table = { Name = "Jessica", Age = 19 },
    Function = function() print("Hi") end
}

treeTable(table)

输出 :

Number  -> <number> 1
String  -> <string> Hello world
Table   -> <table> table: 009E94A8
Boolean -> <boolean> true
Function    -> <function> function: 009EBA10

但我希望输出类似于这样:

Number      -> <number> 1
String      -> <string> Hello world
Table       -> <table> table: 009E94A8
Boolean     -> <boolean> true
Function    -> <function> function: 009EBA10

但我不知道该如何做到这一点,我在 StackOverflow 上搜索了一下,但没有找到对我有用的东西 :/

点赞