「实现代码」
---
-- @function: 打印table的内容,递归
-- @param: tbl 要打印的table
-- @param: level 递归的层数,默认不用传值进来
-- @param: filteDefault 是否过滤打印构造函数,默认为是
-- @return: return
function PrintTable( tbl , level, filteDefault)
local msg = ""
filteDefault = filteDefault or true --默认过滤关键字(DeleteMe, _class_type)
level = level or 1
local indent_str = ""
for i = 1, level do
indent_str = indent_str.." "
end
print(indent_str .. "{")
for k,v in pairs(tbl) do
if filteDefault then
if k ~= "_class_type" and k ~= "DeleteMe" then
local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
print(item_str)
if type(v) == "table" then
PrintTable(v, level + 1)
end
end
else
local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
print(item_str)
if type(v) == "table" then
PrintTable(v, level + 1)
end
end
end
print(indent_str .. "}")
end
local x = {a = 20,20,60,{a = {a = 1,2323},2323}}
PrintTable(x)
用sublime直接ctrl+B编译运行,就可以看到控制台里正确的输出了table的内容。
{
1 = 20
2 = 60
3 = table: 006DB668
{
1 = 2323
a = table: 006DB7D0
{
1 = 2323
a = 1
}
}
a = 20
}
网友评论