惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
Y
Y Combinator Blog
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
V
V2EX
腾讯CDC
F
Fortinet All Blogs
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss

博客园 - 毛尹航

GAMES202课程资源 游戏设计制作配置表原则 Unity中文字和图片混合 UI仔需要了解的坑 遇到大段ifelse不要慌,先分层让条件正交 NGUI文字破碎 一个可以用的Lua的Class函数 关于C#的接口的碎碎念 C#中接口是值类型还是引用类型? Unity3d笔试题大全 FPSCalc——简单FPS观测类 GameObjectPool——Unity中的对象池 MonoSingleton——Unity中的单例模式 用非递归、不用栈的方法,实现原位(in-place)的快速排序 一道有序洗牌的笔试题,阿里\UC等都用过 - 毛尹航 - 博客园 MFC中显示图像的放大、缩小、移动功能 MFC中设置对话框/窗体大小固定 MFC【5】MFC集合类 MFC【6】文件I/O和串行化
写一个可以用的Lua打印Table的函数
毛尹航 · 2021-07-08 · via 博客园 - 毛尹航
function PrintTable(node)
    if not node or type(node) ~= "table" then
        return tostring(node)
    end
    local depthBufferHelper = {}
    local function tab(amt)
        if not depthBufferHelper[amt] then
            local t = {}
            for i = 1, amt do
                table.insert(t,"\t")
            end
            depthBufferHelper[amt] = table.concat(t)
        end
        return depthBufferHelper[amt]
    end
    local bufferHelper = {}
    local function __P(_node,_depth,_buffer)
        bufferHelper[_node] = true
        table.insert(_buffer,tab(_depth-1))
        table.insert(_buffer," {\n")
        for k,v in pairs(_node)  do
            local output = {}
            table.insert(output,tab(_depth))
            if (type(k) == "number" or type(k) == "boolean") then
                table.insert(output,"[")
                table.insert(output,tostring(k))
                table.insert(output,"]")
            else
                table.insert(output,"['")
                table.insert(output,tostring(k))
                table.insert(output,"']")
            end

            table.insert(output," = ")
            table.insert(output,tostring(v))
            table.insert(output,"\n")
            table.insert(_buffer,table.concat(output))
            if (type(v) == "table") then
                if bufferHelper[v] == nil then
                    __P(v,_depth + 1,_buffer)
                end
            end
        end
        table.insert(_buffer,tab(_depth-1))
        table.insert(_buffer," }\n")
    end

    local depth = 1
    local buffer = {}
    __P(node,depth,buffer)
    return table.concat(buffer)
end

这个函数会拼一个字符串,并不是真的print什么东西,所以吐槽名字的,嗯,我知道……

但是用还是可以用的,换上你们熟悉的名字,加个debug.traceback什么的在log里算是比较OK了。

其实写Lua已经很长时间了,也奇怪网上为什么都是不能用的打印函数。因为都是菜鸡?

还是大家都喜欢调试而不喜欢用log?关键调试和log应用场景也不一样啊。

对了,这个函数格式化出来会像这样:

看出来了么?第二个打印出来的table里有循环引用。这也是我为什么说没看到有好用的函数的原因,

不是循环引用没处理,就是处理了循环引用,但字符串格式崩了。

循环引用的测试代码长这样:

local t1 = {}
local t2 = {}
local t3 = {}
t1.next = t2
t2.next = t3
t3.next = t2
print(PrintTable(t1))

以上。