Skip to content

Instantly share code, notes, and snippets.

@rtauziac
Last active August 29, 2015 14:07
Create a string representing the full description of a Lua table.
function fullDescription(aTable, depth, descriptionString)
if depth == nil then
depth = 1
end
if descriptionString == nil then
descriptionString = ""
end
local negSpace = ""
local tabSize = 4
for i = tabSize + 1, depth * tabSize do
negSpace = negSpace.." "
end
for key, value in pairs(aTable) do
if type(value) == "table" then
descriptionString = descriptionString..negSpace.."|"..key.."\n"
descriptionString = fullDescription(value, depth+1, descriptionString)
else
descriptionString = descriptionString..negSpace.."|"..key.." : "..tostring(value).."\n"
end
end
return descriptionString
end
--[[
local root = {
name = "racine",
options = {
sound = true,
music = false,
difficulty = "easy"
},
player = {
health = 20
}
}
print(fullDescription(root)) -- prints:
|name : racine
|options
|sound : true
|music : false
|difficulty : easy
|player
|health : 20
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment