Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR LUA

lua print table as string

-- Usage: GetTableString(table) | "depth" is recursive param, ignore it.
-- This function will print the table in formatted way.
function GetTableString(o, depth)
    if type(o) ~= "table" then
        return "Given parameter is not a table."
    end

    local pad = function(str, msg, depth)
        local padding = string.rep(str, depth)
        return padding..msg
    end
    
    depth  = depth or 0
    local tabLen = (depth + 1) * 4
    local displayStr = ""
    
    displayStr = displayStr..pad(" ", "{", 0).."
"
    for k,v in pairs(o) do
        displayStr = displayStr..pad(" ", k.." = ", tabLen)
        
        if type(v) == "table" then
            displayStr = displayStr..DebugUtil.getTableString(v, depth+1)
        else
            displayStr = displayStr..tostring(v).." ("..type(v)..")"
        end
        
        displayStr = displayStr.."
"
    end
    displayStr = displayStr..pad(" ", "}", depth * 4).."
"
    
    return displayStr
end
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #lua #print #table #string
ADD COMMENT
Topic
Name
1+4 =