Created
March 17, 2011 17:54
-
-
Save hashmal/874792 to your computer and use it in GitHub Desktop.
[Lua] Print table contents recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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 |
Other values also may fail to concat with string. Use tostring(v).
https://gist.github.com/xytis/5361405
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
marked
- Avoid extracting already-printed tables to prevent infinite loop for self-referential tables such as
_G - Format strings like literals to improve readability and preserve indentation
https://gist.github.com/Phroneris/56c1edaf3186b09d289184b8188098a4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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