Skip to content

Instantly share code, notes, and snippets.

@walterlua
Created June 17, 2011 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterlua/1030993 to your computer and use it in GitHub Desktop.
Save walterlua/1030993 to your computer and use it in GitHub Desktop.
Utility method for printing the contents of a variable, especially tables
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