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"}
相关推荐
信号处理学渣11 分钟前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客12 分钟前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
jasmine s21 分钟前
Pandas
开发语言·python
biomooc42 分钟前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人44 分钟前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
black^sugar1 小时前
纯前端实现更新检测
开发语言·前端·javascript
404NooFound1 小时前
Python轻量级NoSQL数据库TinyDB
开发语言·python·nosql
用余生去守护2 小时前
python报错系列(16)--pyinstaller ????????
开发语言·python
数据小爬虫@2 小时前
利用Python爬虫快速获取商品历史价格信息
开发语言·爬虫·python
向宇it2 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎