table.print = function(root,depthMax,excludeKey,excludeType)
assert(type(root)=="table","无法打印非table")
depthMax = depthMax or 3
local cache = { [root] = "." }
local depth = 0
print("{")
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if type(k) == "string" then
key ='\"' .. tostring(k) .. '\"'
end
if type(v) == "table" then
if cache[v] then
table.insert(temp,space .. "["..key.."]" .." = " .. " {" .. cache[v].."},")
else
local new_key = name .. "." .. tostring(k)
cache[v] = new_key .." ->[".. tostring(v) .."]"
depth = depth + 1
if depth>=depthMax or (excludeKey and excludeKey==k) then
table.insert(temp,space .. "["..key.."]" .." = " .. "{ ... }")
else
local tableStr = _dump(v,space .. (next(t,k) and "|" or " ") .. string.rep(" ",#key<4 and 4 or #key),new_key)
if tableStr then
table.insert(temp,space .. "["..key.."]" .." = " .. "{")
table.insert(temp, tableStr)
table.insert(temp,space .."},")
else
table.insert(temp,space .. "["..key.."]" .." = " .. "{ },")
end
end
depth = depth -1
end
else
local vType = type(v)
if not excludeType or excludeType~=vType then
if vType == "string" then
v = '\"' .. v .. '\"'
else
v = tostring(v) or "nil"
end
table.insert(temp,space .. "["..key.."]" .. " = " .. v ..",")
end
end
end
return #temp>0 and table.concat(temp,"\n") or nil
end
local allTableString = _dump(root, " ","")
print(allTableString or "")
print("}")
return allTableString
end
local a = {1,2,3,4,5,6,7,8,9}
table.print(a)