Lua打印表格

function print_table(t)
local cache = {}
local function print_table_sub (t, indent)
if (cache[tostring(t)]) then
print(indent .. "*" .. tostring(t))
else
cache[tostring(t)] = true
if (type(t) == "table") then
for pos, val in pairs(t) do
if (type(val) == "table") then
print(indent .. "[" .. pos .. "] => " .. tostring(t) .. " {")
print_table_sub (val, indent .. string.rep(" ", string.len(pos) + 8))
print(indent .. string.rep(" ", string.len(pos) + 6) .. "}")
elseif (type(val) == "string") then
print(indent .. "[" .. pos .. '] => "' .. val .. '"')
else
print(indent .. "[" .. pos .. "] => " .. tostring(val))
end
end
else
print(indent .. tostring(t))
end
end
end
if (type(t) == "table") then
print(tostring(t) .. " {")
print_table_sub (t, " ")
print("}")
else
print_table_sub (t, " ")
end
print()
end