Lua - 输出打印table表
lua自带的print函数只能打印可转化为字符串的数据,如果打印table表的话,则会和打印函数和userdata类型数据一样,输出为内存地址的形式。工作中因项目需要,可打印table表的话,对bug的查修和信息的监控将会高效的多。
打印table表的方法如下
-- log输出格式化
local function logPrint(str)str = os.date("\nLog output date: %Y-%m-%d %H:%M:%S \n", os.time()) .. strprint(str)
end-- key值格式化
local function formatKey(key)local t = type(key)if t == "number" thenreturn "["..key.."]"elseif t == "string" thenlocal n = tonumber(key)if n thenreturn "["..key.."]"endendreturn key
end-- 栈
local function newStack()local stack = {tableList = {}}function stack:push(t)table.insert(self.tableList, t)endfunction stack:pop()return table.remove(self.tableList)endfunction stack:contains(t)for _, v in ipairs(self.tableList) doif v == t thenreturn trueendendreturn falseendreturn stack
end-- 输出打印table表 函数
function printTable(...)local args = {...}for k, v in pairs(args) dolocal root = vif type(root) == "table" thenlocal temp = {"------------------------ printTable start ------------------------\n","local tableValue".." = {\n",}local stack = newStack()local function table2String(t, depth)stack:push(t)if type(depth) == "number" thendepth = depth + 1elsedepth = 1endlocal indent = ""for i=1, depth doindent = indent .. " "endfor k, v in pairs(t) dolocal key = tostring(k)local typeV = type(v)if typeV == "table" thenif key ~= "__valuePrototype" thenif stack:contains(v) thentable.insert(temp, indent..formatKey(key).." = {检测到循环引用!},\n")elsetable.insert(temp, indent..formatKey(key).." = {\n")table2String(v, depth)table.insert(temp, indent.."},\n")endendelseif typeV == "string" thentable.insert(temp, string.format("%s%s = \"%s\",\n", indent, formatKey(key), tostring(v)))elsetable.insert(temp, string.format("%s%s = %s,\n", indent, formatKey(key), tostring(v)))endendstack:pop()endtable2String(root)table.insert(temp, "}\n------------------------- printTable end -------------------------")logPrint(table.concat(temp))elselogPrint("----------------------- printString start ------------------------\n".. tostring(root) .. "\n------------------------ printString end -------------------------")endend
end
测试:
local testTb1 = {"cc", "dd", ["a"] = {1}}
local testTb = {"aa", ["bb"] = {1, 3, ["b"] = "test"}, "cc", "dd"}
testTb1["a"][2] = testTb1
printTable(testTb)
printTable(testTb1)
printTable("当前测试时间:" .. os.date("%Y-%m-%d %H:%M:%S", os.time()))
测试结果:
Log output date: 2020-09-01 15:19:04
------------------------ printTable start ------------------------
local tableValue = {[1] = "aa",[2] = "cc",[3] = "dd",bb = {[1] = 1,[2] = 3,b = "test",},
}
------------------------- printTable end -------------------------Log output date: 2020-09-01 15:19:04
------------------------ printTable start ------------------------
local tableValue = {[1] = "cc",[2] = "dd",a = {[1] = 1,[2] = {检测到循环引用!},},
}
------------------------- printTable end -------------------------Log output date: 2020-09-01 15:19:04
----------------------- printString start ------------------------
当前测试时间:2020-09-01 15:19:04
------------------------ printString end -------------------------
从测试结果可以看出,准确且完整的输出了table中的内容,并对循环引用做出了相应处理。
Lua - 输出打印table表相关推荐
- Lua语言之table表
table有两种形式,数组形式与键值对形式. 一:table基本用法 1.table基本使用 ①初始化 table 表名 = {} ②赋值 数组形式: newTable[0] = "BeiJ ...
- Lua table(表)
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- Lua之table(表)
Lua table(表) 使用表来统一表示Lua中的一切数据,是Lua区分于其他语言的一个特色.这个特色从最开始的Lua版本保持至今,很大的原因是为了在设计上保持简洁.Lua表分为数组和散列表部分,其 ...
- 打印100~200 之间的素数,输出乘法口诀表,判断1000年---2000年之间的闰年
1.打印100~200 之间的素数 #include <stdio.h> int main() { int n,i; for(i=100;i<=200;i=i++) { for(n= ...
- Lua打印table的工具函数dump
在 Lua 中,print 是不能直接打印出 table 里面的数据的,如果用循环遍历打印遇到嵌套 table 的话也是很不友好的! local tblTest = {1, a = {111, 222 ...
- javascript实现输出打印九九乘法表、水仙花数、
javascript输出打印九九乘法表 for(var i = 1;i<=9;i++){ for(var j = 1;j<=i;j++){document.write(j + '*' + ...
- 第六章第十五题(金融应用:打印税表)(Financial application: print a tax table)
第六章第十五题(金融应用:打印税表)(Financial application: print a tax table) *6.15(金融应用:打印税表)程序清单3-5给出了计算税款的程序.使用税款的 ...
- Lua 中 table(表) 的简单使用
Lua table(表) table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数字.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能 ...
- Lua 打印table、ngx.say table
目录 打印 table函数: ngx.say table 函数: 打印 table函数: function print_r ( t )local print_r_cache={}local funct ...
最新文章
- 为何多线程就能提高Java程序的执行效率
- h5实现手机端等级进度条
- android事件分发笔记
- 华南农业大学c语言期末试题,华南农业大学珠学院C语言期末试卷.doc
- Go的strconv一
- react 中渲染html_如何在React中识别和解决浪费的渲染
- 个人或结对项目 - 动态显示程序运算的过程
- java web 开发之写在前面(0)
- 句柄泄漏与应用程序体验查找服务(AELookupSvc)
- 自学编程的 6 个误区 【原力计划-打卡挑战】第一周榜单揭晓
- 体脂率在线计算机,在线体脂率计算器 男性15%~18%若体脂率过
- 利用PMOS实现LED恒流驱动芯片的通断控制
- 为Kong添加服务、路由和认证
- 大数据_数据来源类型
- 移动文件夹ubuntu
- 腾讯云直播介绍及如何设置直播
- 加密文件的识别和破解工具,电子数据勘察取证实验室建设项目-掘密
- mysql5.7.23绿色版安装
- VBIDE.VBE的应用
- 测试TensorFlow 是否安装成功