Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Code for pretty printing a table (for example the table containing all global variables)

edited November 2011 in Code Sharing Posts: 4

-- print contents of a table, with keys sorted. second parameter is optional, used for indenting subtables
function dump(t,indent)
    local names = {}
    if not indent then indent = "" end
    for n,g in pairs(t) do
        table.insert(names,n)
    end
    table.sort(names)
    for i,n in pairs(names) do
        local v = t[n]
        if type(v) == "table" then
            if(v==t) then -- prevent endless loop if table contains reference to itself
                print(indent..tostring(n)..": <-")
            else
                print(indent..tostring(n)..":")
                dump(v,indent.."   ")
            end
        else
            if type(v) == "function" then
                print(indent..tostring(n).."()")
            else
                print(indent..tostring(n)..": "..tostring(v))
            end
        end
    end
end
-- Use this function to perform your initial setup
function setup()
    dump(_G) -- _G is the globals table in lua
end
-- This function gets called once every frame
function draw()
end

Comments

  • SimeonSimeon Admin Mod
    Posts: 5,400

    Nice! Very informative output.

  • BortelsBortels Mod
    Posts: 1,557

    I love this - very handy!

    But I want more - with persistent data coming down the pipe any day now, we're going to want serialization; and since the persistence isn't 8-bit clean, we can't play tricky games with memory blobs (not that we can get to that anyway) So what we really want is what perl's Data::Dumper does - something that spits out nice, human-readable machine-parsible source code.

    This is another "I'm not requesting it because we can roll our own" project. Someone out there has to have run into and solved, and shared - we just have to find it and adapt it, and make it our own. And paste it into every project until we can get shared libraries :-)

    To the googles!

  • Posts: 4

    there are plenty of implementations here: http://lua-users.org/wiki/TableSerialization

Sign In or Register to comment.