Skip to content

Instantly share code, notes, and snippets.

@toriaezunama
Created August 13, 2012 23:10
Show Gist options
  • Save toriaezunama/3344709 to your computer and use it in GitHub Desktop.
Save toriaezunama/3344709 to your computer and use it in GitHub Desktop.
print recursive function #lua #moai #corona
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
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)+]] 4 ))
print(indent..string.rep(" ",--[[string.len(pos)+6]] 4 ) .."}")
elseif (type(val)=="string") then
print(indent.."[".. tostring( pos ) ..'] => "'..val..'"')
else
print(indent.."[" .. tostring( pos ) .."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment