Created
April 25, 2016 14:50
-
-
Save dpino/af37d70554d157bbee289f489945cce5 to your computer and use it in GitHub Desktop.
Print_r in Lua
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
-- 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