Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Adding an array to the end of another

pacpac
in Questions Posts: 79

So I am having trouble getting my code to add arrays so could you please write who to do this

Here is something like my code

Array1. :this is the big array
Array2. :this is the small array

for I = 1, 30
Array1[I + #Array1]
for j = 1, 6
Array1[I + #Array1][j] = Array2[i][j]
Please help. Thanks Pac

Tagged:

Comments

  • pacpac
    Posts: 79

    Sorry if code above is bad it remove the white space I put in sorry

  • Posts: 1,014

    @pac - you can assign a variable to the size of array 1 using sz = Array1. Then you can append array 2 using table.insert with sz+ a loop counter. If you post a little code we can make more pointed suggestions.

  • dave1707dave1707 Mod
    Posts: 7,155

    @pac If you want to add one table to the end of another, try this.

    function setup()
        -- create 2 tables
        tab1={1,2,3,4,5}
        tab2={6,7,8,9,10}
    
        -- add table 2 to the end of table 1
        for z=1,#tab2 do    
            table.insert(tab1,tab2[z])
        end
    
        -- print table 1
        print(table.concat(tab1," "))
    end
    
  • pacpac
    Posts: 79

    Thanks guys I will try those two things

  • Posts: 1,014

    @pac - please read the reference for table.insert. Without specifying it new table entries are appended. The full syntax is table.insert(tab1,pos,value) where value, in your case is tab2[z] and pos defaults to #tab1+1. As each entry is inserted #tab1 is automatically incremented so #tab1+1 always points to the next table entry point ie to append the next value in the table.

  • pacpac
    Posts: 79

    Thanks guys I got it working now.

Sign In or Register to comment.