美文网首页
lua 打印递归打印table

lua 打印递归打印table

作者: AnruoLuo | 来源:发表于2018-06-13 16:05 被阅读0次

    -- @function: 打印table的内容,递归
    -- @param: tbl 要打印的table
    -- @param: level 递归的层数,默认不用传值进来
    -- @param: filteDefault 是否过滤打印构造函数,默认为是
    -- @return: return
    function PrintTable( tbl , level, filteDefault)
    local msg = ""
    filteDefault = filteDefault or true --默认过滤关键字(DeleteMe, _class_type)
    level = level or 1
    local indent_str = ""
    for i = 1, level do
    indent_str = indent_str.." "
    end

    print(indent_str .. "{")
    for k,v in pairs(tbl) do
    if filteDefault then
    if k ~= "_class_type" and k ~= "DeleteMe" then
    local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
    print(item_str)
    if type(v) == "table" then
    PrintTable(v, level + 1)
    end
    end
    else
    local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
    print(item_str)
    if type(v) == "table" then
    PrintTable(v, level + 1)
    end
    end
    end
    print(indent_str .. "}")
    end

    local x = {a = 20,20,60,{a = {a = 1,2323},2323},tab11={1,2,1,3}}
    PrintTable(x)

    相关文章

      网友评论

          本文标题:lua 打印递归打印table

          本文链接:https://www.haomeiwen.com/subject/pknjeftx.html