Lua打印table中所有數據

function LuaHelps.PrintTable(t, name)
    local spaceAdd = 4
    local function getTableStr(t, name, space)
        local str = string.format("%s%s = {\n", string.rep(" ", space - spaceAdd), (name or "table"))
        local init = false
        for k, v in pairs(t) do
            if type(v) == "table" then
                str = str .. getTableStr(v, k, space + spaceAdd)
            else
                if type(v) == "string" and string.len(v) > 2 and string.sub(v, 1, 3) == "cs." then
                    init = true
                end
                str = str .. string.format("%s%s = %s\n", string.rep(" ", space), k, v)
            end
        end
        str = str .. string.rep(" ", space - spaceAdd) .. "}\n"
        return str
    end
    if (type(t) == "table") then
        print("\n" .. getTableStr(t, name, spaceAdd))
    else
        print(t)
    end
end

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章