Small Basic: Array Basics

This article covers the basic use of the Small Basic Arrays.

What is an Array

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.

my_array[1] = 5

Below is a statement that uses this array item and sets the variable 'result' to be the contents of my_array[1].  After this statement result will contain the value 5.

result = my_array[1]

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.

balls[1] = Shapes.AddEllipse(20,20)
balls[2] = Shapes.AddEllipse(20,20)
balls[3] = Shapes.AddEllipse(20,20)
balls[4] = Shapes.AddEllipse(20,20)
balls[5] = Shapes.AddEllipse(20,20)
balls[6] = Shapes.AddEllipse(20,20)
balls[7] = Shapes.AddEllipse(20,20)
balls[8] = Shapes.AddEllipse(20,20)
balls[9] = Shapes.AddEllipse(20,20)
balls[10] = Shapes.AddEllipse(20,20)
For i =  1 To 10
  balls[i] = Shapes.AddEllipse(20,20)
EndFor

Special Features of Small Basic Arrays

Clearing an array or array element

To clear an array element just set it equal to "".  The array element will then be deleted from the array. 

my_array[5] = ""

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 "".

my_array  = ""

Copying arrays

An entire array may be copied to another new array by simply asigning it using =.

copy_array  = my_array

Array methods

There are several methods in the Array class, the most useful returns the size (or number of elements) of the array.

size = Array.GetItemCount(my_array)

The ContainsIndex and ContainsValue methods simply test whether the specified index or value is present in the array.

Non integer indexing

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.

age["Fred"] = 14
age["Mary"] = 12
age["John"] = 22

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.

indices  = Array.GetAllIndices(age)
For i =  1 To Array.GetItemCount(indices)
  name = indices[i]
  TextWindow.WriteLine(name+" is "+age[name]+" years old")
EndFor

Multi-dimensional arrays

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)

contact["Fred"]["age"] = 14
contact["Fred"]["sex"] = "M"
contact["Fred"]["email"] = "fred@email.com"
contact["Mary"]["age"] = 12
contact["Mary"]["sex"] = "F"
contact["Mary"]["email"] = "mary@email.com"

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.

age = "Fred=14;Mary=12;John=22;"

See Also


Other Languages