How to add to an array
-
Say I have an array variable
var.array1 = {0,1,2,3}
Now I want to add another element to the array. Arrays are fixed length, so I need to create a new array with the contents of the first array, plus my new element.
I've tried things like
var array2 = {var.array1,"new element"}
var array2 = {var.array1^"new element"}
var array2 = {{var.array1}^"new element"}
Etc.But I can't find an expression that uses the elements of array 1 as the first elements of a new array.
-
@mikeabuilder
You can add the first array as the first element of a new array like thisvar myArray = {1,2,3,4,} var myArray2 = {{var.myArray},5}
but that might not make it what you want
You may need to use a loop if you want it all as single elements
;initial array var myArray = {1,2,3,4,} echo var.myArray ; second array one element larger var myArray2 = vector(#var.myArray+1,null) ;copy first to second while iterations < #var.myArray set var.myArray2[iterations] = var.myArray[iterations] ;set last element in second array set var.myArray2[#var.myArray2-1]=#var.myArray2 echo var.myArray2 ;make first array bigger set var.myArray = vector(#var.myArray+1,null) ; copy second array back into first set var.myArray = var.myArray2 echo var.myArray
-
@OwenD - VECTOR!!!! Excellent. I didn;t know that was added. It's precisely what I need. It allows me to create an array of a given length that I can then fill (as you demonstrate).
Thanks!
-
-