树形打印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_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",
        },
    },
},
时间: 2024-08-06 06:55:13

树形打印lua table表的相关文章

Lua之table(表)

Lua table(表) 使用表来统一表示Lua中的一切数据,是Lua区分于其他语言的一个特色.这个特色从最开始的Lua版本保持至今,很大的原因是为了在设计上保持简洁.Lua表分为数组和散列表部分,其中数组部分不像其他语言那样,从0开始作为第一个索引,而是从1开始.散列表部分可以存储任何其他不能存放在数组部分的数据,唯一的要求就是键值不能为nil.尽管内部实现上区分了这两个部分,但是对使用者而言却是透明的.使用Lua表,可以模拟出其他各种数据结构--数组.链表.树等. 虽然设计上简洁,并且对使用

Lua获取表中字段的名称

假设有下面这段代码 local t = { a = 1, b = { x = 1, y = 2} } 我传给你一个table, 想要知道这个table都有哪些字段,但是又不能直接获取,这时可以使用下面这个方法 for k, v in pairs (t) do print(tostring(k), v) end 可以看到, 把k用tostring函数转成字段串形式就OK了 但是我们还可以看到,当table中嵌套table的时候,嵌套的table无法打印出来,对于这种情况,难道就没有办法了吗?当然不

lua实现深度拷贝table表

lua当变量作为函数的参数进行传递时,类似的也是boolean,string,number类型的变量进行值传递.而table,function,userdata类型的变量进行引用传递.故而当table进行赋值操作之时,table A 赋值给table B,对表B中元素进行操作自然也会对A产生影响,当然对B表本身进行处理例如B =nil或者将表B指向另一个表,则对A是没什么影响的:下面即是对lua table的深度拷贝. deepcopy = function(object) local look

Lua的Table表使用例子(便于使用查询)

一.table.insert() 1.1 1 local countries = {"China", "England", "Brazil"} 2 --尾插法(Pos不填,默认插入尾部) 3 table.insert(countries, "France") 4 --头插法(首部插入) 5 table.insert(countries, 1, "Australia") 6 7 dump(countries)

递归打印lua中的table

递归打印lua中的table,并写到文件里: 1 local pairs_by_keys = function(inTable) 2 local temp = {} 3 for k, v in pairs(inTable) do 4 temp[#temp + 1] = k 5 end 6 7 local compare = function(a, b) 8 if type(a) == type(b) then 9 return a < b 10 elseif type(a) == "num

lua 数据以table表的形式存储

出处:http://blog.sina.com.cn/s/blog_991afe570101rkfh.html 感谢,原文作者的无私奉献! 实现思路: table表 ------> 转为字符串(这里利用json库)------->存储--------->读取 达成目的: 解决字符串拼接问题 用最简单办法将table表存入文件,方便游戏存档.游戏关卡等等 第一步:将table表转为json格式的字符串 导入头文件 --2.2.5的quick版本已经在框架载入时完成了json模块的初始化,所

lua table integer index 特性

table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.) 返回表中最大的正数值index. 说明: 1. 此接口不是统计表中元素的

Lua table pair和ipair区别

官方描述: ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction for i,v in ipairs(t) do body end will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table. pairs (

Lua查找表元素过程(元表、__index方法是如何工作的)(转载)

文章来源于 Lua查找表元素过程(元表.__index方法是如何工作的) Lua的表本质其实是个类似HashMap的东西,其元素是很多的Key-Value对,如果尝试访问了一个表中并不存在的元素时,就会触发Lua的一套查找机制,也是凭借这个机制,才能够实现“面向对象”的. 举例说明: tempTable = {} print(tempTable.memberA) --这里试图打印tempTable并不存在的成员memberA 执行结果:nil输出为nil的原因很简单,tempTable中并没有m