lua递归打印table

代码在文章末尾,先看两个用法

例子1

local t = {
    {1,2,3},
    {{4,5,6},['a']='a'},
    {[123]=123,['123']='123'},
    {['a']=1,{['b']=2, {['c']=3}}}
}
myModule.my_print_table_recursive(t, "想要打印的信息")

例子2:打印指定字段

local t = {
    {['name']="zhangSan",   ['age']=7,  ['gender']="male"},
    {['name']="liSi",       ['age']=8,  ['gender']="female"},
    {['name']="wangWu",     ['age']=9,  ['gender']="male"}
}
myModule.my_print_table_recursive(t, "想要打印的信息", {_in={'name','age'}})
local myModule = {}

function myModule.tablefind(tbl, val)
    if tbl == nil or val == nil or type(tbl) ~= "table" then
        return nil
    end

    for i, v in pairs(tbl) do
        if v == val then
            return i
        end
    end

    return nil
end

-- getRealValue 如果val为string类型则给他加上双引号,便于更好识别当key为数字时两种不同类型number和string的区别
function myModule.getRealValue(val)
    if type(val) == "string" and not string.find(val, "\"") then
        val = "\""..val.."\""
    end
    return val
end


---my_print_table_recursive
---@param t table
---@param str string    需要打印出来的提示信息
---@param fileds table={_in={},_out={}}  _in需要打印的字段, _out不需要打印的字段
function myModule.my_print_table_recursive ( t, str, fileds)
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do

                    if fileds and fileds._out and myModule.tablefind(fileds._out, pos) then
                        goto continue
                    end
                    if fileds and fileds._in and not myModule.tablefind(fileds._in, pos) then
                        goto continue
                    end

                    if (type(val)=="table") then
                        print(indent.."["..myModule.getRealValue(pos).."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    elseif (type(val)=="string") then
                        print(indent.."["..myModule.getRealValue(pos)..'] => "'..val..'"')
                    else
                        print(indent.."["..myModule.getRealValue(pos).."] => "..tostring(val))
                    end

                    ::continue::
                end
            else
                print(indent..tostring(t))
            end
        end
    end

    print("\n***** print begin *********   ", str)
    if (type(t)=="table") then
        print(tostring(t).." {")
        sub_print_r(t,"  ")
        print("}")
    else
        sub_print_r(t,"  ")
    end
    print("***** print end   *********   ", str, "\n")
end

---my_print_table_normal
---@param ta table
---@param str string    需要打印出来的提示信息
---@param fileds table={_in={},_out={}}  _in需要打印的字段, _out不需要打印的字段
function myModule.my_print_table_normal(ta, str, fileds)
    print("\n***** print begin *********   ", str)
    if type(ta) == "table" then
        for k,v in pairs(ta) do
            if fileds and fileds._out and myModule.tablefind(fileds._out, k) then
                goto continue
            end
            if fileds and fileds._in and not myModule.tablefind(fileds._in, k) then
                goto continue
            end

            print("key= "..myModule.getRealValue(k),"value= ",myModule.getRealValue(v))

            ::continue::
        end
    end
    print("***** print end   *********   ", str, "\n")
end

return myModule

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值