Skip to content

Instantly share code, notes, and snippets.

@Habbie
Created September 14, 2021 13:17
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 Habbie/2d7789d3520006eab63769f208c7d129 to your computer and use it in GitHub Desktop.
Save Habbie/2d7789d3520006eab63769f208c7d129 to your computer and use it in GitHub Desktop.
local to_string2
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, to_string2(key) .. "={\n");
table.insert(sb, table_print (value, indent + 2, done))
table.insert(sb, string.rep (" ", indent)) -- indent it
table.insert(sb, "}\n");
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
function to_string2( tbl )
if "nil" == type( tbl ) then
return tostring(nil)
elseif "table" == type( tbl ) then
return table_print(tbl)
elseif "string" == type( tbl ) then
return tbl
else
return tostring(tbl)
end
end
print(to_string2( {['a']={'b'}}))
@Habbie
Copy link
Author

Habbie commented Sep 14, 2021

a={
  1 = "b"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment