Categories: Lua 教程

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..{

        local separator = ""
        local entry = "
"
        local barray = true
        local kk,vv
        table.foreachi (iauxTable, function (i, k)
            if i == k and barray then
                entry = __tostring(value[k], indent..   , vmap)
                str = str..separator..indent..   ..entry
                separator = ", 
"
            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..   ,vmap)

            str = str..separator..indent..   ..entry
            separator = ", 
"
        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..   ,vmap)

            str = str..separator..indent..   ..entry
            separator = ", 
"
        end)

        str = str..
..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)

打印效果如下:

唐伯虎点蚊香

前端小白,想各位学习!

Share
Published by
唐伯虎点蚊香

Recent Posts

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 天 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

1 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

2 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

2 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

3 周 ago