Populating an Array
There are several ways to populate an array. The example code following each of these methods creates the exact same array, with the same elements:
Use CreateArray when you initially create the array:
Local Array of Number &MyArray; &MyArray = CreateArray(100, 200, 300);
Assign values to the elements of the array:
Local Array of Any &MYARRAY; &MYARRAY = CreateArray(); &MYARRAY[1] = 100; &MYARRAY[2] = 200; &MYARRAY[3] = 300;
Note: Using CreateArray without any parameters creates an array of Any.
Use the Push method to add items to the end of the array:
Local Array of Number &MYARRAY; Local Number &MYNUM; &MYARRAY = CreateArrayRept(&MYNUM, 0); /* this creates an empty array of number */ &MYARRAY.Push(100); &MYARRAY.Push(200); &MYARRAY.Push(300);
Use the Unshift method to add items to the beginning of the array:
Local Array of Number &MYARRAY; Local Number &MYNUM; &MYARRAY = CreateArrayRept(&MYNUM, 0); /* this creates an empty array of number */ &MYARRAY.Unshift(300); &MYARRAY.Unshift(200); &MYARRAY.Unshift(100);
You can also use CreateArrayRept (repeat) when you initially create the array. The following example creates an array with three elements: all three elements have the same data, that is, 100:
Local Array of Number &MYARRAY;
&MYARRAY = CreateArrayRept(100, 3);