在 Lua 中,print 是不能直接打印出 table 里面的数据的,如果用循环遍历打印遇到嵌套 table 的话也是很不友好的!

local tblTest = {1, a = {111, 222}, "ccc"}print(tblTest)
-- table: 00A17E20for k, v in pairs(tblTest) doprint(k, v)
end
--[[
1   1
2   ccc
a   table: 00A17C40
]]

在 Cocos2d-x 的工具函数集 functions.lua 中,提供了一个打印 table 的工具函数 dump,记录如下:

function dump_value_(v)if type(v) == "string" thenv = "\"" .. v .. "\""endreturn tostring(v)
endfunction split(input, delimiter)input = tostring(input)delimiter = tostring(delimiter)if (delimiter == "") then return false endlocal pos, arr = 0, {}for st, sp in function() return string.find(input, delimiter, pos, true) end dotable.insert(arr, string.sub(input, pos, st - 1))pos = sp + 1endtable.insert(arr, string.sub(input, pos))return arr
endfunction trim(input)return (string.gsub(input, "^%s*(.-)%s*$", "%1"))
end--[[
打印table的工具函数
@params value 需要打印的内容
@params desciption 描述
@params nesting 打印内容的嵌套级数,默认3级
]]
function dump(value, desciption, nesting)if type(nesting) ~= "number" then nesting = 3 endlocal lookupTable = {}local result = {}local traceback = split(debug.traceback("", 2), "\n")-- print("dump from: " .. trim(traceback[3]))local function dump_(value, desciption, indent, nest, keylen)desciption = desciption or "<var>"local spc = ""if type(keylen) == "number" thenspc = string.rep(" ", keylen - string.len(dump_value_(desciption)))endif type(value) ~= "table" thenresult[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(desciption), spc, dump_value_(value))elseif lookupTable[tostring(value)] thenresult[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(desciption), spc)elselookupTable[tostring(value)] = trueif nest > nesting thenresult[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(desciption))elseresult[#result +1 ] = string.format("%s%s = {", indent, dump_value_(desciption))local indent2 = indent.."    "local keys = {}local keylen = 0local values = {}for k, v in pairs(value) dokeys[#keys + 1] = klocal vk = dump_value_(k)local vkl = string.len(vk)if vkl > keylen then keylen = vkl endvalues[k] = vendtable.sort(keys, function(a, b)if type(a) == "number" and type(b) == "number" thenreturn a < belsereturn tostring(a) < tostring(b)endend)for i, k in ipairs(keys) dodump_(values[k], k, indent2, nest + 1, keylen)endresult[#result +1] = string.format("%s}", indent)endendenddump_(value, desciption, "- ", 1)for i, line in ipairs(result) doprint(line)end
end

test:

-- string test
local strTest = "hello world!"
dump(strTest)
-- - "<var>" = "hello world!"-- table test
local tblTest = {a = {one = 111, two = 222}, b = "666", c = {{id = 1, num = 123}, {id = 2, num = 456}}}
dump(tblTest, "table print->")
--[[
- "table print->" = {
-     "a" = {
-         "one" = 111
-         "two" = 222
-     }
-     "b" = "666"
-     "c" = {
-         1 = {
-             "id"  = 1
-             "num" = 123
-         }
-         2 = {
-             "id"  = 2
-             "num" = 456
-         }
-     }
- }
]]tblTest = {{nesting1 = 1}, {nesting2 = 2, {nesting3 = 3, {nesting4 = 4, {nesting = 5}}}}}
dump(tblTest, "nesting test->")  -- 默认最多只打印3级
--[[
- "nesting test->" = {
-     1 = {
-         "nesting1" = 1
-     }
-     2 = {
-         1 = {
-             1 = *MAX NESTING*
-             "nesting3" = 3
-         }
-         "nesting2" = 2
-     }
- }
]]
dump(tblTest, "nesting test->", 5)
--[[
- "nesting test->" = {
-     1 = {
-         "nesting1" = 1
-     }
-     2 = {
-         1 = {
-             1 = {
-                 1 = {
-                     "nesting" = 5
-                 }
-                 "nesting4" = 4
-             }
-             "nesting3" = 3
-         }
-         "nesting2" = 2
-     }
- }
]]

Lua打印table的工具函数dump相关推荐

  1. Lua 打印table、ngx.say table

    目录 打印 table函数: ngx.say table 函数: 打印 table函数: function print_r ( t )local print_r_cache={}local funct ...

  2. Lua - 输出打印table表

    lua自带的print函数只能打印可转化为字符串的数据,如果打印table表的话,则会和打印函数和userdata类型数据一样,输出为内存地址的形式.工作中因项目需要,可打印table表的话,对bug ...

  3. Lua 中 table 库函数 table.concat 连接 函数

    Lua 中 table 库函数 table.concat 连接 函数 do--> table.concat 连接 函数 tab = {"a", "c", ...

  4. Lua 的table遍历 【转】

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rangercyh.blog.51cto.com/1444712/1032925 ...

  5. 如何打印出lua里table的内容

    不像开发as3时用fb有强大的断点调试功能,一般lua开发不用什么高级的ide,貌似也没有适合的,就直接用sublime.exvim等文本编辑器,直接编译运行看结果.所以不能很方便的知道变量值,特别是 ...

  6. gookit/goutil - Go一些常用的工具函数实现、增强、收集和整理

    gookit/goutil Go 常用的一些工具函数,数字,字符串,数组,Map,文件,错误,时间日期,特殊处理,格式化,常用信息获取等等 工具包 arrutil array/slice 相关操作的函 ...

  7. lua和unity如何交互_【Lua与C#交互④】如何让Lua打印到Unity控制台

    今天要讲的如何让Lua打印到Unity控制台? 相信用过tolua或者xlua的人都知道,在lua脚本里面只要写一行print就能打印到unity控制台效果类似Debug.Log. 如下: print ...

  8. 从零开始学习jQuery (九) jQuery工具函数

    本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...

  9. 从零开始学习jQuery (九) jQuery工具函数 【转】

    一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案,  即使你会使用jQuery也能在阅读中发现些许秘籍. 我们经常要使用脚本处理各种业务逻辑, 最常见的就 ...

最新文章

  1. 语义分割--Loss Max-Pooling for Semantic Image Segmentation
  2. Windows Server下DB2自动备份、复原和前滚的脚本编程
  3. spring cloud云服务架构 - particle云架构代码结构讲解
  4. 鸿蒙是安卓换皮UI,鸿蒙2.0是安卓换皮?还真不是
  5. 鸿蒙系统的挑战,简单的讲解下何为鸿蒙系统,可能会挑战你的认知颠覆你的想象...
  6. python创建长度为n的数组_在Python中建立N维数组并赋初值
  7. 「管理数学基础」2.4 泛函分析:有界线性算子与泛函、例题
  8. ajax请求进error怎么弹出错诶信息,在ajax请求jqgrid之后出现错误时显示错误消息...
  9. unity2018关联不到vs_Unity2018 VS2017打开CS脚本,提示全红及无法加载工程等问题解决...
  10. NXP RT1052 eFlexPWM—灵活的增强型 PWM
  11. 云主服务器排行榜_国内云服务器排名
  12. 1653 藏头诗
  13. ASP.NET 安全认证(三)—— 用Form 表单认证实现单点登录(Single Sign On)
  14. 前端实现在线预览文档(pdf、doc文件)
  15. 微信小程序投诉页面与交互设计
  16. 拥抱变化 把握未来——“移动”电商营销新时代
  17. 调整物料的存货科目代码,会不会造成存货系统和总账对账不平?
  18. IBM继续统领超级计算 Opteron发展迅速
  19. 白杨流量汇线下聚会品茶聊流量,搞流量的核心是什么?
  20. logit方程怎么写_呆哥数学坐标系与参数方程——最全概括【1】

热门文章

  1. 读书笔记:《七周七数据库》
  2. vip结尾的域名到期查询,多久会被删除?
  3. 基于51单片机的智能垃圾桶
  4. Eclipse 的常用快捷键
  5. 真果科技董事长贾求真女士应邀出席2019APEC女性领导力论坛
  6. yum 有趣的linux命令,Centos6中yum方法安装sl(linux有趣命令之一sl跑火车)
  7. 世界杯视频直播:电信运营商的一次互联网战争
  8. 腾讯员工绩效被打2星,情绪接近崩溃!大厂“内卷”太凶残......
  9. Linux信号控制-sigprocmask,sigsuspend,sigpending,sigaction,sigqueue
  10. 基于MATLAB的LPF低通滤波器设计(巴特沃斯)