Дамп значения переменных для отладки


Две очень полезные фукции для дампа таблиц и переменных
(Используются для отладки по аналогии с var_dump() и print_r() из PHP)


x = {
    tag = 'root',
    { tag = 'item', 'a' },
    { tag = 'item', 'b' }
}

function tprint(tbl, indent)
    local fmt = ''
    if indent==nil then indent = 0 end
    if tbl==nil then return end
    local space = string.rep("  ", indent)
    if type(tbl) == 'table' then
        fmt = space .. tostring(tbl)
        print(fmt)
        for k, v in pairs(tbl) do
            fmt = space .. k .. ":: "
            print(fmt)
            tprint(v, indent+1)
        end
    elseif type(tbl) == 'boolean' then
        fmt = space .. 'boolean' .. ":: " .. tbl
        print(fmt)
    elseif type(tbl) == 'number' then
        fmt = space .. 'number' .. ":: " .. tbl
        print(fmt)
    elseif type(tbl) == 'string' then
        fmt = space .. 'string' .. ":: " .. tbl
        print(fmt)
    elseif type(tbl) == 'function' then
        fmt = space .. 'function' .. ":: " .. tbl
        print(fmt)
    elseif type(tbl) == 'thread' then
        fmt = space .. 'thread' .. ":: " .. tbl
        print(fmt)
    elseif type(tbl) == 'userdata' then
        fmt = space .. 'userdata' .. ":: " .. tbl
        print(fmt)
    else
        fmt = space .. 'unknown' .. ":: " .. tbl
        print(fmt)
    end

end

function tpath(tbl, pth, idx)
    if idx==nil then
        idx = ''
    end
    if pth==idx then
        return tbl
    end
    if type(tbl)=='table' then
        for k, v in pairs(tbl) do
            local x = tpath(v, pth, idx..'/'..k)
            if x~=nil then
                return x
            end
        end
    end
    return nil
end

tprint(x)

tprint(tpath(x,'/1/tag'))