Skip to content

Instantly share code, notes, and snippets.

@jasonbradley
Created December 15, 2012 05:22
Show Gist options
  • Save jasonbradley/4291505 to your computer and use it in GitHub Desktop.
Save jasonbradley/4291505 to your computer and use it in GitHub Desktop.
PHP print_r for Lua
Util.print_r = function(t, indent, done)
done = done or {}
indent = indent or ''
local nextIndent -- Storage for next indentation value
for key, value in pairs (t) do
if type (value) == "table" and not done [value] then
nextIndent = nextIndent or
(indent .. string.rep(' ',string.len(tostring (key))+2))
-- Shortcut conditional allocation
done [value] = true
print (indent .. "[" .. tostring (key) .. "] => Table {");
print (nextIndent .. "{");
print_r (value, nextIndent .. string.rep(' ',2), done)
print (nextIndent .. "}");
else
print (indent .. "[" .. tostring (key) .. "] => " .. tostring (value).."")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment