Skip to content

Instantly share code, notes, and snippets.

@dpino
Created April 25, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpino/af37d70554d157bbee289f489945cce5 to your computer and use it in GitHub Desktop.
Save dpino/af37d70554d157bbee289f489945cce5 to your computer and use it in GitHub Desktop.
Print_r in Lua
-- Possible solution to http://stackoverflow.com/questions/36834786/how-to-save-a-table-to-a-file-from-lua/36836187#36836187
function print_r (t, fd)
fd = fd or io.stdout
local function print(str)
str = str or ""
fd:write(str.."\n")
end
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
function savetxt(t, filename)
local fd = io.open(filename, "wt")
print_r(t, fd)
fd:close()
end
local t = {
test = "test",
}
savetxt(t, "test.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment