Skip to content

Instantly share code, notes, and snippets.

@hashmal
Created March 17, 2011 17:54
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save hashmal/874792 to your computer and use it in GitHub Desktop.
Save hashmal/874792 to your computer and use it in GitHub Desktop.
[Lua] Print table contents recursively
-- Print contents of `tbl`, with indentation.
-- `indent` sets the initial level of indentation.
function tprint (tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
else
print(formatting .. v)
end
end
end
@rsheep
Copy link

rsheep commented Mar 7, 2013

if there's a "boolean" value in the table the "print(formatting .. v)" fails.
https://gist.github.com/ripter/4270799
this is the same solution I came by

@xytis
Copy link

xytis commented Apr 11, 2013

Other values also may fail to concat with string. Use tostring(v).
https://gist.github.com/xytis/5361405

@stuby
Copy link

stuby commented Apr 23, 2013

This is what I've been using. Real handy for understanding arbitrary nested stuff.
The Code: https://gist.github.com/stuby/5445834#file-rprint-lua
An example: https://gist.github.com/stuby/5445834#file-rprint-example-txt

@jfzlnyf
Copy link

jfzlnyf commented Apr 15, 2014

marked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment