Arrays of data parts

EGL can group variables of the same type into arrays. This topic includes the basic information that you need to use arrays in EGL.
An array is an ordered series of variables of the same type. For example, you can define an array of integer variables:
myInts int[] = [1,2,3,4,5];
In this case, the array is a series of five integer variables. In EGL, the numbering of arrays begins with the number one, so this array has elements numbered one through five.
You can access each of the integers in the array as though they were individual variables by specifying the index number of the integer in brackets:
myInts[1] = 5+5;
myInts[2] = 16;
myInts[3] = myInts[1] + myInts[2];
You can also assign values to the array more than one at a time by using one of these two methods: To assign values to the array when you create it, use either of these two methods:
myStringsInit string[] {"Hello", "Goodbye"};
myBigIntsInit bigint[] = [10, 40];
To assign properties to the array and specify starting values in the set-value block, put the property name-value pairs after the starting values:
myDecimals decimal(10,2)[3] {55.43, 22.12, 4.34, CurrencySymbol = "$"};
If you are using the array literal method to specify starting values, you can set properties with the set-value block as usual:
myBools boolean[3]{MaxSize = 5} = [true, false, true];
If you specify a number of elements in the array when you create it, that array is initialized to contain that number of elements. Each element has the default value for its type:
fiveInts int[5];
SysLib.writeStderr(fiveInts[1]); //Writes "0"
When you create an array, specify a starting length for the array so that EGL can initialize it. You can add or remove elements later with array functions such as appendElement and removeElement.
If you do not specify a starting length for the array, it starts as null, so there is nothing in the array to access:
nullArray int[];
nullArray[2] = 5; //NullValueException!
nullArray.appendElement(5); //NullValueException!
nullArray {1,2,3}; //NullValueException!
Instead, you must begin by initializing the array with an array literal:
nullArray2 int[];
nullArray2 = [1,2,3];
nullArray2.appendElement(4);
Alternatively, you can use a set-value block to initialize a null array:
emptyArray int[]{};
emptyArray.appendElement(5);
In the previous example, the set-value block is empty. You cannot use a set-value block to assign more elements to an array than currently exist in the array. This array has zero elements, so you must use a set-value block with zero values.
You can increase the length of an array by assigning a longer array literal to it. In this case, you overwrite the shorter array with a new, longer array. The following example assigns an array literal with 5 elements to replace an array with 2 elements:
smallIntArray int[2];
smallIntArray = [1,2,3,4,5];
You cannot use a set-value block to assign more elements to the array than currently exist in the array:
smallStrArray string[2];
smallStrArray {"ab", "cd", "ef", "gh"}; 
//IndexOutOfBoundsException! Array has only 2 elements!

For more details about arrays, see "Arrays" in the Reference section.


Feedback