Array Data Type

Arrays are numbered lists of values. One may address individual values in the list by indexing. If the variable arr is an array, one may reference the fifth element by using five as an index in square brackets, like this:

    fifthElement = arr[5];

Note that it is illegal to refer to an array element that has not been given a value.

In Basus, arrays automatically extend as values are added to them. Assigning values to array items is done by indexing with an array as the left hand side of the assignment:

    arr[5] = "foo";

The function maxIndex will return the highest valid index for an array, in other words the highest index that has been assigned a value. This may be used for looping over array elements, like this:

    for index = 1 to maxIndex(arr) do
        println(arr[index]);
    done;

Note that Basus doesn't set any rules for what the first index of an array should be. In some languages arrays are indexed from one. In others they are indexed from zero. In Basus, you may pick any value for the start index, depending on personal preferences and what best matches what you intend to do.

The individual values of an array may have any type, including arrays. All the following statements are valid:

    arr[1] = 42;
    arr[2] = 3.14;
    arr[3] = TRUE;
    arr[4] = "Basus";

    other_arr[1] = arr;

Note how the last statement above assigns the arr array to the first element of the other_arr array. The latter array thus becomes a multi-dimensional array. Given a multi-dimensional array, you may index into underlying arrays by listing several indexes in square brackets, separated by commas. Based on the assignments above, the following would print Basus:

    println(other_arr[1, 4]);

This would change the fourth value of the arr array to foo by indexing through the other_arr array:

    other_arr[1, 4] = "foo";