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

news/2024/10/18 9:16:28/

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

ccmlog.lua

lua">local function __tostring(value, indent, vmap)local str = ''indent = indent or ''vmap = vmap or {}--递归结束条件if (type(value) ~= 'table') thenif (type(value) == 'string') then--字符串str = string.format("[[%s]]", value)else--整数str = tostring(value)endelseif type(vmap) == 'table' thenif vmap[value] then return '('..tostring(value)..')' endvmap[value] = trueendlocal auxTable = {}     --保存元表KEY(非整数)local iauxTable = {}    --保存元表valuelocal iiauxTable = {}   --保存数组(key为0)table.foreach(value, function(i, v)if type(i) == 'number' thenif i == 0 thentable.insert(iiauxTable, i)elsetable.insert(iauxTable, i)endelseif type(i) ~= 'table' thentable.insert(auxTable, i)endend)table.sort(iauxTable)str = str..'{\n'local separator = ""local entry = "\n"local barray = truelocal kk,vvtable.foreachi (iauxTable, function (i, k)if i == k and barray thenentry = __tostring(value[k], indent..'  \t', vmap)str = str..separator..indent..'  \t'..entryseparator = ", \n"elsebarray = falsetable.insert(iiauxTable, k)endend)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'..entryseparator = ", \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'..entryseparator = ", \n"end)str = str..'\n'..indent..'}'endreturn str
endccmlog = function(m,fmt,...)local args = {...}for k,arg in ipairs(args) doif type(arg) == 'table' or type(arg) == 'boolean' or type(arg) == 'function' or type(arg) == 'userdata' thenargs[k] = __tostring(arg)endendargs[#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 = 1local p = CCLOGWARN if m == 'error' then p = CCLOGERROR elseif m == 'warn' then p = CCLOGWARNendwhile 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"}

http://www.ppmy.cn/news/1427901.html

相关文章

Python学习笔记1:变量命名

跟学极客时间的教程系列笔记&#xff1a; 1&#xff09; Python 还支持更灵活的动态解包语法。只要用星号表达式 &#xff08;*variables&#xff09;作为变量名&#xff0c;它便会贪婪 地捕获多个值对象&#xff0c;并将捕获到的内容作为 列表赋值给 variables。比如&#…

Java测试编程题

题目1 1.创建5个线程对象 线程名设置为&#xff08;Thread01&#xff0c;Thread02&#xff0c;Thread03&#xff0c;Thread04&#xff0c;Thread05&#xff09;使用 代码实现5个线程有序的循环打印&#xff0c;效果如下&#xff1a; Thread01正在打印1 Thread02正在打印2 Threa…

python如何对resample+ndarray进行切片提取数据

目录 1. 代码 2. 代码解读2.1. 设置重采样的时间间隔2.2. 对 DataFrame 进行重采样2.3. 将 Resampler 对象转换为数组2.4. 遍历特定时间点并提取数据2.5 总结 1. 代码 # Specfiy time-averageing-interval time_interval 10min df_Tai df.resample(time_interval) np_Tai n…

PDF被加密无法打印的解决办法

思路很清晰&#xff1a;先解密→再打印 分享四个工具&#xff0c;可以轻松解密PDF&#xff1a; ⭐i love pdf I LOVE PDF是一款免费的PDF网站&#xff0c;界面设计简洁&#xff0c;首页没有广告&#xff0c;但每个功能的操作界面是有广告的&#xff0c;不会影响使用。 部分功…

力扣练习题(2024/4/17)

1 最长递增子序列 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的 子…

【网站项目】学习资料销售平台 小程序

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

matlab使用教程(46)—绘制条形图

1.条形图种类 如果需要查看一段时间内的结果、对比不同数据集的结果&#xff0c;或展示单个元素对汇总量的贡献和影响&#xff0c;则条形图会很有用处。 默认情况下&#xff0c;条形图会将一个向量或矩阵中的每个元素表现为一个条形&#xff0c;条形的高度与元素的值成比例。…

怎样将excel的科学计数法设置为指数形式?

对了&#xff0c;这个问题中所谓的“指数形式”是指数学上书写的右上标的指数格式&#xff0c;能不能通过单元格设置来做这个格式的转换呢&#xff1f; 一、几个尝试 以下&#xff0c;以数字123000为例来说明。 情况1.转换成数学上的书写方式&#xff0c;如下图的样子&#x…