Last active
August 29, 2015 14:07
-
-
Save rtauziac/31ba4a5fc6690a8aa432 to your computer and use it in GitHub Desktop.
Create a string representing the full description of a Lua table.
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
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