This article covers the basic use of the Small Basic Arrays.
Any array is a variable that stores a collection of things. Usually the things stored are of the same type, perhaps a list of people's names, or the position of some sprites in a game.
An array has an index and a value for each element in the array. The index is often just a number and the value is the thing stored in the array. The notation used by Small Basic is to put the index of an array element inside square brackets.
Below is an assignment statement, where the value of an array called 'my_array' at index 1 is set to the value 5.
Below is a statement that uses this array item and sets the variable 'result' to be the contents of my_array. After this statement result will contain the value 5.
Arrays are particularly useful when used with number indices that are part of a for loop. For example, both segments of code below are equivalent and initialise an array of 10 ball shapes.
To clear an array element just set it equal to "". The array element will then be deleted from the array.
Note that this may leave a 'hole' in the array indexing, for example if you remove element 5 from an array with 10 elements, then the array will then have 9 elements indexed by 1, 2, 3, 4, 6, 7, 8, 9 and 10.
Note also that if the array element is a GraphicsWindow shape, then the shape itself is not deleted, only its name that was stored in the array.
An entire array can be deleted by setting it to "".
An entire array may be copied to another new array by simply asigning it using =.
There are several methods in the Array class, the most useful returns the size (or number of elements) of the array.
The ContainsIndex and ContainsValue methods simply test whether the specified index or value is present in the array.
In Small Basic the index does not have to be an integer number. They do require to be unique - 2 elements of an array cannot have the same index.
The advantage of non integer indexing is that another index type may be more natural, for example ages of people indexed by their name.
The disadvantage of this is that we cannot index easily over all elements of the array in a for loop. This is also true for arrays where some elements have been deleted or any array where the indices are not a contiguous list of integers. Fortuanately
in these cases there is a way to obtain another array of the indices present. This new array is indexed sequentially by intergers starting at 1, and each value is set to an index in the original array.
In the example below 'indices' is the array of indices in the array 'age'. We can then print the name and age of each person in a for loop.
It is easy to use arrays with more than 1 dimension, simply add more idices in square brackes, but be careful they don't become too large (see Internal structure and Performance)
All arrays in Small Basic are actually stored as a string of indices and values, and internally in a dictionary structure. This allows the non-integer indexing and the single type variables used by Small Basic, but comes at the expense of performance.
Finding an array element from an index that is not neccessarily an integer is a slow process, requiring a near complete search of the array each time an element is accessed. Large arrays, especially multi-dimensional arrays with more than about 100 elements
start to become slow and very slow with several hundred elements.
Sometimes a knowledge of the string indexing format can be helpful. Basically for each element the format is "index=value;". For example, the age array above appears as a string "Fred=14;Mary=12;John=22;". This can actually be used to initialise the array.