Skip to content

Instantly share code, notes, and snippets.

@xfguo
Forked from nrk/gist:31175
Last active August 29, 2015 14:01
Show Gist options
  • Save xfguo/ca9b45397ea50da20788 to your computer and use it in GitHub Desktop.
Save xfguo/ca9b45397ea50da20788 to your computer and use it in GitHub Desktop.
A function in Lua similar to PHP's print_r
-- A function in Lua similar to PHP's print_r, from http://luanet.net/lua/function/print_r
function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
pos = tostring(pos)
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
sub_print_r(t," ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment