打印表数据改进版
发布时间:2020-12-14 22:55:34 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 --------------------------------------------------------------------- This library defines table_print(tb),table_tostring(tb) for -- printin
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 -------------------------------------------------------------------
-- This library defines table_print(tb),table_tostring(tb) for
-- printing table data
-------------------------------------------------------------------
local function _list_table(tb,table_list,level)
local ret = ""
local indent = string.rep(" ",level*4)
for k,v in pairs(tb) do
local quo = type(k) == "string" and """ or ""
ret = ret .. indent .. "[" .. quo .. tostring(k) .. quo .. "] = "
if type(v) == "table" then
local t_name = table_list[v]
if t_name then
ret = ret .. tostring(v) .. " -- > ["" .. t_name .. ""]n"
else
table_list[v] = tostring(k)
ret = ret .. "{n"
ret = ret .. _list_table(v,level+1)
ret = ret .. indent .. "}n"
end
elseif type(v) == "string" then
ret = ret .. """ .. tostring(v) .. ""n"
else
ret = ret .. tostring(v) .. "n"
end
end
local mt = getmetatable(tb)
if mt then
ret = ret .. "n"
local t_name = table_list[mt]
if t_name then
ret = ret .. tostring(mt) .. " -- > ["" .. t_name .. ""]n"
else
ret = ret .. indent .. "<metatable> = {n"
ret = ret .. _list_table(mt,level+1)
ret = ret .. indent .. "}n"
end
end
return ret
end
-------------------------------------------------------------------
-- Public functions
-------------------------------------------------------------------
function table_tostring(tb)
local ret = " = {n"
local table_list = {}
table_list[tb] = "Root Table"
ret = ret .. _list_table(tb,1)
ret = ret .. "}"
return ret
end
function table_print(tb)
if type(tb) ~= "table" then
error("Sorry,it's not table,it is " .. type(tb) .. ".")
end
print(tostring(tb) .. table_tostring(tb))
end
-------------------------------------------------------------------
-- For test
-------------------------------------------------------------------
local test_table_2 = {
print,}
local test_table_1 = {
12,22.5,true,infor = {
name = "Jack",age = 26,lifeexp = {
["1986"] = "Both",["2013"] = "Work in Tencet",["2015"] = "Get married"
},wife = "Lucy"
},"Hello test",recu_table = test_table_2,["2"] = 13
}
test_table_2.recu_table = test_table_1
local metatable = {
__index = test_table_2,__add = function(a,b) return 0 end
}
setmetatable(test_table_1,metatable)
function table_test_lib()
table_print(test_table_1)
end
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
