为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据。以下罗列了三种种关于树形打印lua table的方法;
法一
local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next function print_lua_table (lua_table, indent) if not lua_table or type(lua_table) ~= "table" then
return;
end indent = indent or
for k, v in pairs(lua_table) do
if type(k) == "string" then
k = string.format("%q", k)
end
local szSuffix = ""
if type(v) == "table" then
szSuffix = "{"
end
local szPrefix = string.rep(" ", indent)
formatting = szPrefix.."["..k.."]".." = "..szSuffix
if type(v) == "table" then
print(formatting)
print_lua_table(v, indent + )
print(szPrefix.."},")
else
local szValue = ""
if type(v) == "string" then
szValue = string.format("%q", v)
else
szValue = tostring(v)
end
print(formatting..szValue..",")
end
end
end
  以上是一个树形打印lua table 【原方法的链接】的基本源码,虽是参考云风的方法而来,却 不能够支持表内循环(打印出来的样子还是挺符合 一般人的心里预期的);

法二

譬如一下这个例子: 因为表内引用了自身造成循环引用,所以打印方法也就成了 死循环了;

a = {}
a.a = {
hello = {
alpha = ,
beta = ,
},
world = {
foo = "ooxx",
bar = "haha",
root = a,
},
}
a.b = {
test = a.a
}
a.c = a.a.hello

下面是 云风大神 关于lua table树形打印的源码链接;

local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next function print_r(root)
local cache = { [root] = "." }
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
else
tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
end
end
return tconcat(temp,"\n"..space)
end
print(_dump(root, "",""))
end

那么打印出来的效果是这样:

+a+hello+alpha []
| | +beta []
| +world+root {.}
| +bar [haha]
| +foo [ooxx]
+c {.a.hello}
+b+test {.a}

上面的方法,如果摒除去 root = a 这项元素的循环引用,可打印出来的样子是这样的:

["a"] = {
["hello"] = {
["alpha"] = ,
["beta"] = ,
},
["world"] = {
["bar"] = "haha",
["foo"] = "ooxx",
},
},
["c"] = {
["alpha"] = ,
["beta"] = ,
},
["b"] = {
["test"] = {
["hello"] = {
["alpha"] = ,
["beta"] = ,
},
["world"] = {
["bar"] = "haha",
["foo"] = "ooxx",
},
},
},["a"] = {
["hello"] = {
["alpha"] = ,
["beta"] = ,
},
["world"] = {
["bar"] = "haha",
["foo"] = "ooxx",
},
},
["c"] = {
["alpha"] = ,
["beta"] = ,
},
["b"] = {
["test"] = {
["hello"] = {
["alpha"] = ,
["beta"] = ,
},
["world"] = {
["bar"] = "haha",
["foo"] = "ooxx",
},
},
},

法三:

也可以利用lua的序列化将lua表转化为string,再将其给print出来;

-- 序列化tablle表--將表轉化成string
function serialize(obj)
local lua = ""
local t = type(obj)
if t == "number" then
lua = lua .. obj
elseif t == "boolean" then
lua = lua .. tostring(obj)
elseif t == "string" then
lua = lua .. string.format("%q", obj)
elseif t == "table" then
lua = lua .. "{\n"
for k, v in pairs(obj) do
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
end
local metatable = getmetatable(obj)
if metatable ~= nil and type(metatable.__index) == "table" then
for k, v in pairs(metatable.__index) do
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
end
end
lua = lua .. "}"
elseif t == "nil" then
return nil
else
return "-nil-"
--error("can not serialize a " .. t .. " type.")
end
return lua
end

下面附上 lua的反序列化方法(即 将string[内容是lua table]再转会lua table)

-- 反序列化tablle表--將string轉化成table
function unserialize(lua)
local t = type(lua)
if t == "nil" or lua == "" then
return nil
elseif t == "number" or t == "string" or t == "boolean" then
lua = tostring(lua)
else
error("can not unserialize a " .. t .. " type.")
end
lua = "return " .. lua
local func = loadstring(lua)
if func == nil then
return nil
end
return func()
end

树形打印lua table表的更多相关文章

  1. lua table表

    lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...

  2. 关于 lua table表存储函数且运用

    --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意类型的值来做数组的索引,但这个值不能是nil--lua tabl ...

  3. Lua 学习之基础篇四<Lua table(表)>

    table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...

  4. Lua table(表)

    table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...

  5. lua table表判断是否为空

    官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...

  6. 打印Lua的Table对象

    小伙伴们再也不用为打印lua的Table对象而苦恼了, 本人曾也苦恼过,哈哈 不过今天刚完成了这个东西, 以前在网上搜过打印table的脚本,但是都感觉很不理想,于是,自己造轮子了~ 打印的效果,自己 ...

  7. lua实现深度拷贝table表

    lua当变量作为函数的参数进行传递时,类似的也是boolean,string,number类型的变量进行值传递.而table,function,userdata类型的变量进行引用传递.故而当table ...

  8. [lua]紫猫lua教程-命令宝典-L1-01-07. table表

    L1[table]01. table表的定义与赋值 小知识:声明表的例子 xx={}--创建一个空表xx --给这表的元素赋值 test="a" xx[test]="a& ...

  9. 【游戏开发】小白学Lua——从Lua查找表元素的过程看元表、元方法

    引言 在上篇博客中,我们简单地学习了一下Lua的基本语法.其实在Lua中有一个还有一个叫元表的概念,不得不着重地探讨一下.元表在实际地开发中,也是会被极大程度地所使用到.本篇博客,就让我们从Lua查找 ...

随机推荐

  1. openoffice

    cmdcd/cd C:\Program Files (x86)\OpenOffice 4\program soffice -headless -accept="socket,host=127 ...

  2. UnicodeEncodeError: 'ascii' codec can't encode characters in position问题的解决办法

    今天刚开始用ulipad写python代码 代码如下 #! /usr/bin/env python#coding=utf-8a = int(raw_input('请输入一个数:'))if a<1 ...

  3. 【微信要跪】 iOS 应用如何完全支持 IPv6-ONLY 网络?

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. Python 7 —— 扩展与嵌入

    Python 7 —— 扩展与嵌入 所谓扩展是指,在Python当中调用其他语言,由于Python的问题主要是效率,这里的扩展主要是指扩展C C++程序(重点) 所谓嵌入是指,在其他语言当中可以调用P ...

  5. TypeError: matchExpr[type].exec is not a function

    遇到了这个问题,很久没找到答案,后来使用了万能的google,貌似也没找到答案. 详细描述下: 通过使用 $(".select")来选择jqeury对象,没问题. 通过$(&quo ...

  6. HDU 1010 Tempter of the Bone

    题意:从开始位置走到结束位置,恰好走 t 步 YES 否则 NO 搜索题,由于是恰好走到,所以用到了奇偶剪枝 什么是奇偶剪枝,我也是刚知道 所给步数为 t ,起始位置坐标 (begin_x,begin ...

  7. 用canvas制作酷炫射击游戏--part1

    好久没写博客了,因为过年后一直在学游戏制作方面的知识.学得差不多后又花了3个月时间做了个作品出来,现在正拿着这个作品找工作. 作品地址:https://betasu.github.io/Crimonl ...

  8. 1.webService入门

    学习webService前,先来思考一个问题: 请看以下截图: 以上是一个综合网站的部分显示信息,我们能很明显的看出打开该网页所处区域的一些信息,比如:地点是厦门,天气是阵雨,温度是9摄氏度等等... ...

  9. 重绘控件中OnPaint、OnDraw、OnDrawItem和DrawItem的区别

    ==================================================================================================== ...

  10. MSSQL数据库中Text类型字段在PHP中被截断之解 (转)

    在PHP中使用了MSSQL数据库,恰巧数据库中又使用了Text类型字段,于是问题产生了.每次从数据库中查询得到的数据总是被莫名的截断,一开始是以为我使用的PHP框架中对字符串的长度有所限制,后来发现这 ...