Array
An Array is an ordered collection of values.
Creating arrays
An array can be created by using brackets:
You can also initialize an array with values:
Reading values
You can access an Array's index by using the array access operator []:
note
If you access an index that's past the end of the array an error will be triggered.
This is somewhat different from popular dynamic languages, but is done intentionally to avoid undetected bugs. Melon can do this because arrays are a completely different type from Object and share nothing with it, while other popular programming languages use the equivalent of Object for arrays or an extension thereof.
Reading size
You can use the size operator # to retrieve an Array's size:
Adding values
You can add values to an existing array by not passing an index to the array access operator [] as follows:
Writing values
Writing values to existing array indices works as you would expect:
Iterating
Arrays provide a native Iterator that can be used to iterate over them:
Outputs:
You can also iterate over arrays using Range values, for example because the index of the item is needed:
Outputs:
Array slicing
Array values can be sliced returning a new array composed of only a part of the original.
The slicing operator [start:end] can be used for slicing.
The start index is inclusive and the end index is exclusive.
Both the start and end index are optional and the end index can also be a negative index expressing an index starting from the end of the array.
You can use the slicing operator to clone an Array value:
Concatenation
You can combine two arrays in a new array by using the concatenation operator ..:
Further array manipulation
There are other ways to manipulate Array values, they can be found in the array core module.