• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[any TFS] print_r, improved print

Zothion

Veteran OT User
Joined
Apr 28, 2014
Messages
1,092
Reaction score
394
whenever i'm coding something and is troubleshooting, print_r (which is a function in php, ported to lua here) is a godsend for checking any problems with tables. it iterates through an entire table and prints all values/subvalues in good formatting, it makes it so much easier to test things

none of these was made by me, i just think everyone should have this function (both gotten from https://gist.github.com/nrk/31175)

basic one
Code:
function print_r ( t ) 
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    sub_print_r(t,"  ")
end

more advanced one (prints types/memory address etc instead of just values
Code:
-- works like PHP's print_r(), returning the output instead of printing it to STDOUT
-- daniel speakmedia com

function dumpvar(data)
    -- cache of tables already printed, to avoid infinite recursive loops
    local tablecache = {}
    local buffer = ""
    local padder = "    "

    local function _dumpvar(d, depth)
        local t = type(d)
        local str = tostring(d)
        if (t == "table") then
            if (tablecache[str]) then
                -- table already dumped before, so we dont
                -- dump it again, just mention it
                buffer = buffer.."<"..str..">\n"
            else
                tablecache[str] = (tablecache[str] or 0) + 1
                buffer = buffer.."("..str..") {\n"
                for k, v in pairs(d) do
                    buffer = buffer..string.rep(padder, depth+1).."["..k.."] => "
                    _dumpvar(v, depth+1)
                end
                buffer = buffer..string.rep(padder, depth).."}\n"
            end
        elseif (t == "number") then
            buffer = buffer.."("..t..") "..str.."\n"
        else
            buffer = buffer.."("..t..") \""..str.."\"\n"
        end
    end
    _dumpvar(data, 0)
    return buffer
end


--[[
print(dumpvar({
    [1] = "this is the first element",
    [2] = "and this is the second",
    [3] = 3,
    [4] = 3.1415,
    ["nested"] = {
        ["lua"] = "is cool but",
        ["luajit"] = "is fucking awesome!"
    },
    ["some-files"] = {
        [0] = io.stdin,
        [1] = io.stdout,
        [2] = io.stderr
    }
}))

-- outputs:

(table: 0x41365f60) {
    [1] => (string) "this is the first element"
    [2] => (string) "and this is the second"
    [3] => (number) 3
    [4] => (number) 3.1415
    [nested] => (table: 0x41365fb0) {
        [code=lua] => (string) "is cool but"
        [luajit] => (string) "is fucking awesome!"
    }
    [some-files] => (table: 0x41366048) {
        [0] => (userdata) "file (0x3f1e79b6a0)"
        [1] => (userdata) "file (0x3f1e79b780)"
        [2] => (userdata) "file (0x3f1e79b860)"
    }
}

]]
 
Last edited:
Back
Top