Dump Variables Debugging (code snippet)

Description

Requires: None

Code

Dump.fh_lua
function dump (tt, label,indent, done)
    if debug == true then
       if label == nil then
          label = 'Dump'
       end
        done = done or {}
        indent = indent or 0
        if type(tt) == "table" then
            if indent == 0 then
                io.write(string.rep (" ", indent))
                io.write(label..'\n')
            end
            for key, value in pairs (tt) do
                io.write(string.rep (" ", indent)) -- indent it
                if type (value) == "table" and not done [value] then
                    done [value] = true
                    io.write(string.format("[%s] => table\n", tostring (key)));
                    io.write(string.rep (" ", indent+4)) -- indent it
                    io.write("(\n");
                    dump (value, tostring(key),indent + 7, done)
                    io.write(string.rep (" ", indent+4)) -- indent it
                    io.write(")\n");
                else
                    io.write(string.format("[%s] => %s\n",
                    tostring (key), tostring(value)))
                end
            end
        else
            io.write(tostring(label)..':'..tostring(tt))
        end
    else
        return
    end
end

Usage

1st parameter the variable to dump, optional second parameter the string to describe it

---------------------------------- Top of Program
debug = true   -- comment out to turn off dump function
----------------------------------- main code
a = {1,2,3,4,5,6,7,8,9,10}
b = 'hello world'
d = {a,b}
dump(d,'Table D')

Output Window

 Table D
 [1] => table
     (
        [1] => 1
        [2] => 2
        [3] => 3
        [4] => 4
        [5] => 5
        [6] => 6
        [7] => 7
        [8] => 8
        [9] => 9
        [10] => 10
     )
 [2] => hello world
 Plugin has completed