Arrays of data parts

Like many other programming languages, EGL can group variables of the same type into arrays. This topic covers the basics of using 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, arrays begin numbering 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 using one of these two methods: You can also use either of these two methods to assign values to the array when you create it:
myStringsInit string[] {"Hello", "Goodbye"};
myBigIntsInit bigint[] = [10, 40];
If you want to assign properties to the array as well as specifying starting values in the set-values 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 of specifying starting values, you can set properties with the set-values 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"
It's good coding practice to specify a starting length for the array when you create it so that EGL can initialize it. You can always add or remove elements later with array functions such as appendElement and removeElement.
However, if you do not specify a starting length for the array, it starts as null, so there is nothing in the array to be accessed:
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-values 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-values 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-values 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 only two elements:
smallIntArray int[2];
smallIntArray = [1,2,3,4,5];
However, you cannot use a set-values 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 on arrays, see "Arrays" in the EGL Language Reference.


Feedback