Skip to content

Instantly share code, notes, and snippets.

@jarnesjo
Created May 21, 2018 08:08
Show Gist options
  • Save jarnesjo/ab3e549d86c0152aecd37cd2c201daa9 to your computer and use it in GitHub Desktop.
Save jarnesjo/ab3e549d86c0152aecd37cd2c201daa9 to your computer and use it in GitHub Desktop.
Table to string in lua
function table_to_string(tbl)
local result = "{"
for k, v in pairs(tbl) do
-- Check the key type (ignore any numerical keys - assume its an array)
if type(k) == "string" then
result = result .. "[\"" .. k .. "\"]" .. "="
end
-- Check the value type
if type(v) == "table" then
result = result .. table_to_string(v)
elseif type(v) == "boolean" then
result = result .. tostring(v)
else
result = result .. "\"" .. v .. "\""
end
result = result .. ","
end
-- Remove leading commas from the result
if result ~= "" then
result = result:sub(1, result:len() - 1)
end
return result .. "}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment