Created
May 4, 2018 09:21
-
-
Save cuixin/3a7f88209021ddd0c67e29821232a90a to your computer and use it in GitHub Desktop.
lua print_r test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = { | |
value = 1, | |
text = "test", | |
inest = { | |
aaaa = "1", | |
eee = { | |
abc = 1 | |
}, | |
bl = true, | |
} | |
} | |
function print_r2 (t, name, indent) | |
local tableList = {} | |
function table_r (t, name, indent, full) | |
local id = not full and name | |
or type(name)~="number" and tostring(name) or '['..name..']' | |
local tag = indent .. id .. ' = ' | |
local out = {} -- result | |
if type(t) == "table" then | |
if tableList[t] ~= nil then table.insert(out, tag .. '{} -- ' .. tableList[t] .. ' (self reference)') | |
else | |
tableList[t]= full and (full .. '.' .. id) or id | |
if next(t) then -- Table not empty | |
table.insert(out, tag .. '{') | |
for key,value in pairs(t) do | |
table.insert(out,table_r(value,key,indent .. '| ',tableList[t])) | |
end | |
table.insert(out,indent .. '}') | |
else table.insert(out,tag .. '{}') end | |
end | |
else | |
local val = type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t) | |
table.insert(out, tag .. val) | |
end | |
return table.concat(out, '\n') | |
end | |
return table_r(t,name or 'Value',indent or '') | |
end | |
function print_r(t, indent) | |
indent = indent or "| " | |
for k, v in pairs(t) do | |
local v_t = type(v) | |
if v_t == "string" then | |
print(indent .. k .. " = " .. "\"" .. v .. "\"") | |
elseif v_t == "table" then | |
print(indent .. k .. " = {") | |
print_r(v, indent .. "| ") | |
print(indent .. "}") | |
else | |
print(indent .. k .. " = " .. tostring(v)) | |
end | |
end | |
end | |
print_r(x) | |
print(print_r2(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment