28、Lua 如何输出树状结构的table?

为了让游戏前端数据输出更加条理,做了一个简单树状结构来打印数据。

ccmlog.lua

lua 复制代码
local function __tostring(value, indent, vmap)
    local str = ''
    indent = indent or ''
    vmap = vmap or {}

    --递归结束条件
    if (type(value) ~= 'table') then
        if (type(value) == 'string') then
            --字符串
            str = string.format("[[%s]]", value)
        else
            --整数
            str = tostring(value)
        end
    else
        if type(vmap) == 'table' then
            if vmap[value] then return '('..tostring(value)..')' end
            vmap[value] = true
        end

        local auxTable = {}     --保存元表KEY(非整数)
        local iauxTable = {}    --保存元表value
        local iiauxTable = {}   --保存数组(key为0)
        table.foreach(value, function(i, v)
            if type(i) == 'number' then
                if i == 0 then
                    table.insert(iiauxTable, i)
                else
                    table.insert(iauxTable, i)
                end
            elseif type(i) ~= 'table' then
                table.insert(auxTable, i)
            end
        end)
        table.sort(iauxTable)

        str = str..'{\n'
        local separator = ""
        local entry = "\n"
        local barray = true
        local kk,vv
        table.foreachi (iauxTable, function (i, k)
            if i == k and barray then
                entry = __tostring(value[k], indent..'  \t', vmap)
                str = str..separator..indent..'  \t'..entry
                separator = ", \n"
            else
                barray = false
                table.insert(iiauxTable, k)
            end
        end)
        table.sort(iiauxTable)

        table.foreachi (iiauxTable, function (i, fieldName)

            kk = tostring(fieldName)
            if type(fieldName) == "number" then 
                kk = '['..kk.."]"
            end 
            entry = kk .. " = " .. __tostring(value[fieldName],indent..'  \t',vmap)

            str = str..separator..indent..'  \t'..entry
            separator = ", \n"
        end)
        table.sort(auxTable)

        table.foreachi (auxTable, function (i, fieldName)

            kk = tostring(fieldName)
            if type(fieldName) == "number" then 
                kk = '['..kk.."]"
            end 
            vv = value[fieldName]
            entry = kk .. " = " .. __tostring(value[fieldName],indent..'  \t',vmap)

            str = str..separator..indent..'  \t'..entry
            separator = ", \n"
        end)

        str = str..'\n'..indent..'}'
    end

    return str
end

ccmlog = function(m,fmt,...)
    local args = {...}
    for k,arg in ipairs(args) do
        if type(arg) == 'table' 
            or type(arg) == 'boolean' 
            or type(arg) == 'function' 
            or type(arg) == 'userdata' then
            args[k] = __tostring(arg)
        end
    end

    args[#args+1] = "nil"
    args[#args+1] = "nil"
    args[#args+1] = "nil"
    local str = string.format("[%s]:"..fmt.." %s", m, unpack(args))
    print(str)

    local off = 1
    local p = CCLOGWARN 
    if m == 'error' then 
        p = CCLOGERROR 
    elseif m == 'warn' then 
        p = CCLOGWARN
    end
    while off <= #str do 
        local subStr = string.sub(str, off, off+1024)
        off = off + #subStr
        --p(subStr)
    end
end

--打印测试
reserved =  { [100] = { 300, 400}, 200, { 300, 500}, abc = "abc",[0] = {1,2,3,"abc"}}

ccmlog("d","d",reserved)

打印效果如下:

lua 复制代码
[d]:d {100} = {300, 400}, 200, {300, 500}, abc = "abc",[0] = {1,2,3,"abc"}
相关推荐
Biomamba生信基地2 分钟前
R语言基础| 回归分析
开发语言·回归·r语言
黑客-雨16 分钟前
从零开始:如何用Python训练一个AI模型(超详细教程)非常详细收藏我这一篇就够了!
开发语言·人工智能·python·大模型·ai产品经理·大模型学习·大模型入门
Pandaconda21 分钟前
【Golang 面试题】每日 3 题(三十九)
开发语言·经验分享·笔记·后端·面试·golang·go
加油,旭杏25 分钟前
【go语言】变量和常量
服务器·开发语言·golang
行路见知25 分钟前
3.3 Go 返回值详解
开发语言·golang
xcLeigh29 分钟前
WPF实战案例 | C# WPF实现大学选课系统
开发语言·c#·wpf
NoneCoder39 分钟前
JavaScript系列(38)-- WebRTC技术详解
开发语言·javascript·webrtc
关关钧1 小时前
【R语言】数学运算
开发语言·r语言
十二同学啊1 小时前
JSqlParser:Java SQL 解析利器
java·开发语言·sql
编程小筑1 小时前
R语言的编程范式
开发语言·后端·golang