Arrays - Manhunter07/MFL GitHub Wiki
See also: Type constructors#array
Arrays are lists of enumerated values containing of an indefinite number of sub-values, called elements. Like characters in strings, elements in an array are stored using a zero-based positive number as key, called index.
Notation
An array defined using brackets ([ and ]) surrounding its values. No values written between the brackets ([]) notates an empty array. Arrays can contain any type of elements, including nested arrays. Values are segregated using commas (,) that are placed between each pair of elements. The first element must not be preceded and the last element must not be succeeded by a comma, so it is sometimes necessary to adjust commas when copying or pasting around elements.
Members
As mentioned above, members can be of any type. This includes arrays, but also other types of values including null-values, references, numbers, strings and records. If an array contains other arrays, it is often referred to as either fully or partly multi-dimensional. Members are ordered and accessed by index using a dot (.) as indicator, like for example MyArray.0 to access the first element of MyArray. If an array is multi-dimensional, its nested array elements can be accessed directly, too, using multiple concatenated dot-index notations (ie. MyArray.1.4). If the index is an expression, it should be enclosed with parentheses (ie. MyArray.(i) or MyArray.(Length(MyArray)-1)).
Examples
An array of numbers from 1 to 9 can be declared using a simple bracket notation:
const Digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Once this is done, we can use and put it into another array, together with the number 0 and a reversed negated copy of itself:
var Numbers = [-Reverse(Digits), 0, Digits]
This will result in a Numbers variable defining an array that contains three elements:
- The newly-generated sub-array
[-9, -8, ... -2, -1]using theReversefunction - The number
0 - A reference to the already-existing pre-declared array
Digitswith the values[1, 2, ... 8, 9]
This array is called partly multi-dimensional, because it can be accessed using more than one index after another:
Numbers.0.3 \returns 4\
In this case, parts of this array are two-dimensional. We can now flatten it to one dimension using the Flatten function:
var Numbers = Flatten(Numbers)
The Numbers variable has now been overwritten with a one-dimensional representation of itself, where all nested arrays inside the outer array have been dissolved and their values inserted into the first dimension: [-9, -8, ... -1, 0, 1, ... 8, 9].
Operators
See also: Operators#Arrays