Concatenation of tables in Lua

后端 未结 9 796
难免孤独
难免孤独 2020-12-09 15:04

ORIGINAL POST

Given that there is no built in function in Lua, I am in search of a function that allows me to append tables together. I have google

9条回答
  •  眼角桃花
    2020-12-09 15:20

    Here is an implementation I've done similar to RBerteig's above, but using the hidden parameter arg which is available when a function receives a variable number of arguments. Personally, I think this is more readable vs the select syntax.

    function array_concat(...)
        local t = {}
    
        for i = 1, arg.n do
            local array = arg[i]
            if (type(array) == "table") then
                for j = 1, #array do
                    t[#t+1] = array[j]
                end
            else
                t[#t+1] = array
            end
        end
    
        return t
    end
    

提交回复
热议问题