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

in Questions Posts: 216

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



  • pacpac
    Posts: 216

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

  • Posts: 2,039

    @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: 8,968

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

    function setup()
        -- create 2 tables
        -- add table 2 to the end of table 1
        for z=1,#tab2 do    
        -- print table 1
        print(table.concat(tab1," "))
  • pacpac
    Posts: 216

    Thanks guys I will try those two things

  • Posts: 2,039

    @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: 216

    Thanks guys I got it working now.

Sign In or Register to comment.