如何打印出lua里table的内容
1个回答
展开全部
---
-- @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)
-- @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)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询