Skip to content

Instantly share code, notes, and snippets.

@farrajota
Last active August 25, 2016 09:15
Show Gist options
  • Save farrajota/5170a616e6c643cfbead to your computer and use it in GitHub Desktop.
Save farrajota/5170a616e6c643cfbead to your computer and use it in GitHub Desktop.
save lua table contents to file (useful for logging purposes)
function save_configs(filename, opt)
-- prints the contents of a table into a file
local function table_print (tt, indent, done)
done = done or {}
indent = indent or 0
if type(tt) == "table" then
local sb = {}
for key, value in pairs (tt) do
table.insert(sb, string.rep (" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
table.insert(sb, tostring(key) .. ': ');
table.insert(sb, "{\n");
table.insert(sb, table_print (value, indent + 2, done))
table.insert(sb, string.rep (" ", indent)) -- indent it
table.insert(sb, "}\n");
elseif "number" == type(key) then
table.insert(sb, string.format("\"%s\"\n", tostring(value)))
else
table.insert(sb, string.format(
"%s = \"%s\"\n", tostring (key), tostring(value)))
end
end
return table.concat(sb)
else
return tt .. "\n"
end
end -- local function
-- open file
local file = io.open(filename,"w")
file:write('---------------------------------------------------------------------------------------\n')
file:write('\n')
file:write('Table info.\n')
file:write('\n')
file:write('---------------------------------------------------------------------------------------\n\n\n\n')
file:write(table_print(opt,1))
-- close file
file:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment