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)
打印效果如下:




为了让程序模块调用该打印接口,打印函数前加上module("ccmlog", package.seeall),并将此文件保存在类似lualibs的库目录,然后在程序中用require "ccmlog"来调用该函数了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值