1. 程式人生 > >如何在lua中列印一個數組(table)

如何在lua中列印一個數組(table)

主體思路:通過遞迴遍歷整個table元素輸出

local function ZCLOG(Lua_table)
    -- do
    --     return
    -- end
        local function define_print(_tab,str)
            str = str .. "  "
            for k,v in pairs(_tab) do
                if type(v) == "table" then
                    if not tonumber(k) then
                        print(str.. k .."{")
                    else
                        print(str .."{")
                    end
                    define_print(v,str)
                    print( str.."}")
                else
                    print(str .. tostring(k) .. " " .. tostring(v))
                end
            end
        end
    if type(Lua_table) == "table" then
        define_print(Lua_table," ")
    else
        print(tostring(Lua_table))
    end
end
local a={
 test1={1,2,3,4,5},
 test2={
    ["週一"]=1,
    ["週二"]=2,
    ["週三"]=4,
    ["週四"]="3",
    ["週五"]="5",
},
test3={
    [1]=1,
    [2]=2,
    [3]=4,
    [4]="3",
    [5]="5",
}
}
ZCLOG(a)