Skip to content

Instantly share code, notes, and snippets.

@zenwerk
Created May 7, 2014 05:07
Show Gist options
  • Save zenwerk/3b699e88c8b2a423f4a9 to your computer and use it in GitHub Desktop.
Save zenwerk/3b699e88c8b2a423f4a9 to your computer and use it in GitHub Desktop.
php like print_r function.
-- from http://lua-users.org/wiki/TableSerialization
function print_r (t, name, indent)
local tableList = {}
function table_r (t, name, indent, full)
local serial=string.len(full) == 0 and name
or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']'
io.write(indent,serial,' = ')
if type(t) == "table" then
if tableList[t] ~= nil then io.write('{}; -- ',tableList[t],' (self reference)\n')
else
tableList[t]=full..serial
if next(t) then -- Table not empty
io.write('{\n')
for key,value in pairs(t) do table_r(value,key,indent..'\t',full..serial) end
io.write(indent,'};\n')
else io.write('{};\n') end
end
else io.write(type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"'
or tostring(t),';\n') end
end
table_r(t,name or '__unnamed__',indent or '','')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment