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

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

ccmlog.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)

打印效果如下:

[d]:d {100} = {300, 400}, 200, {300, 500}, abc = "abc",[0] = {1,2,3,"abc"}

相关推荐

  1. 28Lua 如何输出树状结构table

    2024-04-20 21:02:05       33 阅读
  2. Lua数据类型:table

    2024-04-20 21:02:05       31 阅读
  3. el-table 树状表格查询符合条件数据

    2024-04-20 21:02:05       27 阅读
  4. Lua Table

    2024-04-20 21:02:05       59 阅读
  5. 13、Lua table(表)

    2024-04-20 21:02:05       39 阅读
  6. Lua Table(表)

    2024-04-20 21:02:05       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-20 21:02:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-20 21:02:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-20 21:02:05       82 阅读
  4. Python语言-面向对象

    2024-04-20 21:02:05       91 阅读

热门阅读

  1. 29、Lua中的元表与元方法

    2024-04-20 21:02:05       29 阅读
  2. 区块链黑客第五讲:委托调用攻击

    2024-04-20 21:02:05       35 阅读
  3. 区块链的应用场景及优势

    2024-04-20 21:02:05       32 阅读
  4. 微信小程序 input 不能输入特殊字符的方法

    2024-04-20 21:02:05       30 阅读
  5. 富格林:看破虚假交易警惕受害亏空

    2024-04-20 21:02:05       36 阅读
  6. MySQL 开源到商业(一):Sun 公司收购了 MySQL AB

    2024-04-20 21:02:05       34 阅读
  7. OllamaFunctions 学习笔记

    2024-04-20 21:02:05       46 阅读
  8. 说说redis的数据类型

    2024-04-20 21:02:05       34 阅读
  9. Nginx出现403 Forbidden、404 Not Found错误的解决方案

    2024-04-20 21:02:05       28 阅读