码迷,mamicode.com
首页 > 其他好文 > 详细

树形打印lua table表

时间:2014-09-15 12:47:48      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   div   

local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next

function print_lua_table (lua_table, indent)

    if not lua_table or type(lua_table) ~= "table" then
        return;
    end

    indent = indent or 0
    for k, v in pairs(lua_table) do
        if type(k) == "string" then
            k = string.format("%q", k)
        end
        local szSuffix = ""
        if type(v) == "table" then
            szSuffix = "{"
        end
        local szPrefix = string.rep("    ", indent)
        formatting = szPrefix.."["..k.."]".." = "..szSuffix
        if type(v) == "table" then
            print(formatting)
            print_lua_table(v, indent + 1)
            print(szPrefix.."},")
        else
            local szValue = ""
            if type(v) == "string" then
                szValue = string.format("%q", v)
            else
                szValue = tostring(v)
            end
            print(formatting..szValue..",")
        end
    end
end

以上是一个树形打印lua table 【原方法的链接】的基本源码,虽是参考云风的方法而来,却 不能够支持表内循环(打印出来的样子还是挺符合 一般人的心里预期的);

譬如一下这个例子: 因为表内引用了自身造成循环引用,所以打印方法也就成了 死循环了;

a = {}
a.a = {
    hello = {
        alpha = 1 ,
        beta = 2,
    },
    world =  {
        foo = "ooxx",
        bar = "haha",
        root = a,
    },
}
a.b = {
    test = a.a
}
a.c = a.a.hello

下面是 云风大神 关于lua table树形打印的源码链接;

local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next
 
function print_r(root)
    local cache = {  [root] = "." }
    local function _dump(t,space,name)
        local temp = {}
        for k,v in pairs(t) do
            local key = tostring(k)
            if cache[v] then
                tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
            elseif type(v) == "table" then
                local new_key = name .. "." .. key
                cache[v] = new_key
                tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
            else
                tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
            end
        end
        return tconcat(temp,"\n"..space)
    end
    print(_dump(root, "",""))
end

那么打印出来的效果是这样: 

+a+hello+alpha [1]
| |     +beta [2]
| +world+root {.}
|       +bar [haha]
|       +foo [ooxx]
+c {.a.hello}
+b+test {.a}

上面的方法,如果摒除去 root = a 这项元素的循环引用,可打印出来的样子是这样的:

["a"] = {
    ["hello"] = {
        ["alpha"] = 1,
        ["beta"] = 2,
    },
    ["world"] = {
        ["bar"] = "haha",
        ["foo"] = "ooxx",
    },
},
["c"] = {
    ["alpha"] = 1,
    ["beta"] = 2,
},
["b"] = {
    ["test"] = {
        ["hello"] = {
            ["alpha"] = 1,
            ["beta"] = 2,
        },
        ["world"] = {
            ["bar"] = "haha",
            ["foo"] = "ooxx",
        },
    },
},["a"] = {
    ["hello"] = {
        ["alpha"] = 1,
        ["beta"] = 2,
    },
    ["world"] = {
        ["bar"] = "haha",
        ["foo"] = "ooxx",
    },
},
["c"] = {
    ["alpha"] = 1,
    ["beta"] = 2,
},
["b"] = {
    ["test"] = {
        ["hello"] = {
            ["alpha"] = 1,
            ["beta"] = 2,
        },
        ["world"] = {
            ["bar"] = "haha",
            ["foo"] = "ooxx",
        },
    },
},

 

树形打印lua table表

标签:style   blog   http   color   io   os   ar   for   div   

原文地址:http://www.cnblogs.com/jadeboy/p/3972524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!