Created
September 14, 2021 13:17
-
-
Save Habbie/2d7789d3520006eab63769f208c7d129 to your computer and use it in GitHub Desktop.
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
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'}})) |
Author
Habbie
commented
Sep 14, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment