Created
June 17, 2011 07:12
-
-
Save walterlua/1030993 to your computer and use it in GitHub Desktop.
Utility method for printing the contents of a variable, especially tables
This file contains 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
function print2( var, name ) | |
if not name then name = "anonymous" end | |
if "table" ~= type( var ) then | |
print( name .. " = " .. tostring( var ) ) | |
else | |
-- for tables, recurse through children | |
for k,v in pairs( var ) do | |
local child | |
if 1 == string.find( k, "%a[%w_]*" ) then | |
-- key can be accessed using dot syntax | |
child = name .. '.' .. k | |
else | |
-- key contains special characters | |
child = name .. '["' .. k .. '"]' | |
end | |
print2( v, child ) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment