lua 字符串数组输出

do
        function test(...) for i = 1, select('#', ...) do local arg = select(i, ...) print("arg", arg) end end test(1, 2, nil, 4, 5) function test2(...) local arg = {...} for i, v in ipairs(arg) do --for i, v in ipairs{...} do  --也能够正常执行 io.write(string.format("val%d: %d\n", i, v)) end end test2(0, 2, 3, nil, 5) end

执行结果数组

arg    1 arg 2 arg nil arg 4 arg 5 val1: 0 val2: 2 val3: 3
1.select('#', ...) 和 select(“#”, ...)均可以。2.变长的数组中含有nil,循环输出。
相关文章
相关标签/搜索