Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.8 Arrays

Array declaration

Like normal variables, arrays can be declared with the make keyword.
Arrays have the array type.
Example:

make array numbers = [1, 2, 3, 4, 5]

This creates an array of integers with the values 1, 2, 3, 4, 5.
An array expression is a comma separated list of values surrounded by square brackets.

Arrays can have any type of values inside of them.

make array values = [1.1, 2, "Hello!", 4, [1,2,3]]

Accessing values

You can access values in an array with the # operator.
The # operator is followed by the index of the value you want to access.
Example:

make array numbers = [1, 2, 3, 4, 5]
do say (numbers#0) // 1
do say (numbers#1) // 2
do say (numbers#2) // 3
do say (numbers#3) // 4
do say (numbers#4) // 5

The index of an array starts at 0 and ends at length - 1.
The length of an array is the number of values in the array.
Attempting to access an index outside of the array will result in an error.
Example:

make array numbers = [1, 2, 3, 4, 5]
do say (numbers#5) // Error: Index out of bounds

However, you can use negative indexes to access values from the end of the array.

make array numbers = [1, 2, 3, 4, 5]
do say (numbers#-1) // 5
do say (numbers#-2) // 4
do say (numbers#-3) // 3
do say (numbers#-4) // 2
do say (numbers#-5) // 1

You can also use the # operator to set values in an array.
Example:

make array numbers = [1, 2, 3, 4, 5]
set numbers#0 = 10
do say (numbers#0) // 10

Array expressions

Array expressions are used to create arrays.
Array expressions are comma separated lists of values surrounded by square brackets.
Example:

do say ([1, 2, 3, 4, 5])

This will print [1, 2, 3, 4, 5] to the console.

You can also use the # operator on array expressions.
Example:

do say ([1, 2, 3, 4, 5]#0) // 1
do say ([1, 2, 3, 4, 5]#1) // 2
do say ([1, 2, 3, 4, 5]#2) // 3

Pushing and popping elements

You can join two arrays together with the + operator.
Example:

make array numbers = [1, 2, 3, 4, 5]
make array numbers2 = [6, 7, 8, 9, 10]
do say (numbers + numbers2) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using this, we can push elements to the end of an array.

make array numbers = [1, 2, 3, 4, 5]
set numbers += [6]
do say (numbers) // [1, 2, 3, 4, 5, 6]

You can also pop elements from the end of an array.

make array numbers = [1, 2, 3, 4, 5]
set numbers -= 1
do say (numbers) // [1, 2, 3, 4]

Using the - operator, you pop the last element from an array.

Array length

Using autocasting, you can get the length of an array by just converting the array to a number.
More info on this in 2.5 Type casting.

make array numbers = [1, 2, 3, 4, 5]
do say ((int)numbers) // 5

Multidimensional arrays

Arrays can also be multidimensional, this is because they can hold any value.
Example:

make array numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
do say (numbers#0#0) // 1
do say (numbers#1#1) // 5
do say (numbers#2) // [7, 8, 9]
make array numbers = [[[[[[1]]]]]]
do sayln (numbers) // [[[[[[1]]]]]]
do sayln (numbers#0#0#0#0#0#0) // 1