Concatenation of tables in Lua

后端 未结 9 790
难免孤独
难免孤独 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:18

    And one more way:

    for _,v in ipairs(t2) do 
        table.insert(t1, v)
    end
    

    It seems to me the most readable one - it iterates over the 2nd table and appends its values to the 1st one, end of story. Curious how it fares in speed to the explicit indexing [] above

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 15:23

    In general the notion of concatenating arbitrary tables does not make sense in Lua because a single key can only have one value.

    There are special cases in which concatenation does make sense. One such is for tables containing simple arrays, which might be the natural result of a function intended to return a list of results.

    In that case, you can write:

    -- return a new array containing the concatenation of all of its 
    -- parameters. Scaler parameters are included in place, and array 
    -- parameters have their values shallow-copied to the final array.
    -- Note that userdata and function values are treated as scalar.
    function array_concat(...) 
        local t = {}
        for n = 1,select("#",...) do
            local arg = select(n,...)
            if type(arg)=="table" then
                for _,v in ipairs(arg) do
                    t[#t+1] = v
                end
            else
                t[#t+1] = arg
            end
        end
        return t
    end
    

    This is a shallow copy, and makes no attempt to find out if a userdata or function value is a container or object of some kind that might need different treatment.

    An alternative implementation might modify the first argument rather than creating a new table. This would save the cost of copying, and make array_concat different from the .. operator on strings.

    Edit: As observed in a comment by Joseph Kingry, I failed to properly extract the actual value of each argument from .... I also failed to return the merged table from the function at all. That's what I get for coding in the answer box and not testing the code at all.

    0 讨论(0)
  • 2020-12-09 15:28

    To add two tables together do this

        ii=0
    for i=#firsttable, #secondtable+#firsttable do
        ii=ii+1
        firsttable[i]=secondtable[ii]
    end
    

    use the first table as the variable you wanted to add as code adds the second one on to the end of the first table in order.

    • i is the start number of the table or list.
    • #secondtable+#firsttable is what to end at.

    It starts at the end of the first table you want to add to, and ends at the end of the second table in a for loop so it works with any size table or list.

    0 讨论(0)
  • 2020-12-09 15:31

    A simple way to do what you want:

    local t1 = {1, 2, 3, 4, 5}
    local t2 = {6, 7, 8, 9, 10}
    
    local t3 = {unpack(t1)}
    for I = 1,#t2 do
        t3[#t1+I] = t2[I]
    end
    
    0 讨论(0)
  • 2020-12-09 15:34

    Overcomplicated answers much?

    here is my implementation:

    function TableConcat(t1,t2)
        for i=1,#t2 do
            t1[#t1+i] = t2[i]
        end
        return t1
    end
    
    0 讨论(0)
提交回复
热议问题