Lua 语言输出Table详细信息的实现

来源:互联网 发布:淘宝联盟淘客贷款 编辑:程序博客网 时间:2024/05/21 15:47
function PrintTable(tbl, f, bShowSquare)    if type(f) ~= "function" and f ~= nil then        error("The second argument %s should be a function", tostring(f))    end    if type(b) ~= "boolean" and b ~= nil then        error("The third argument %s should be a boolean", tostring(b))    end    p = f or io.write    bShowSquare = bShowSquare or false    if type(tbl) == "number" or            type(tbl) == "function" or            type(tbl) == "boolean" or            type(tbl) == "nil" then        p(tostring(tbl))    elseif type(tbl) == "string" then        p(string.format("%q",o))    elseif type(tbl) == "table" then        p("{\n")        for k,v in pairs(tbl) do            if bShowSquare then p("[") end            PrintTable(k, p, b)            if bShowSquare then p("]") end            p(" = ")            PrintTable(v, p, b)            p(",\n")        end        p("}")    endend
0 0
原创粉丝点击