Arrays, Dim and Redim

Run BASIC supports arrays of 5 dimensions at this time. Arrays must be dimensioned before they can be used. There are two types of arrays. Array names that end with a $ character contain strings. Array names that do not end with a $ character contain numbers.

Possible numeric array names:

myArray()
Nums()
Total()

Possible string array names:

FirstName$()
Address$()
Toys$()
weapons$()

Dim


Dim sets aside memory for an array. In Liberty BASIC, it is not necessary to dimension arrays that do not exceed 11 elements (0 - 10). In Run BASIC, all arrays must be dimensioned before use.

Syntax:
DIM name(size1[, size2...]) - Dimension an array of one or more dimensions of size 

Some code examples using Dim

Dim Nums(23)
Dim Persons$(100,4)
Dim Toys$(30)

Arrays can be dimensioned using a variable for the value.

total = 45
Dim Nums(total)

Zero Indexed


Arrays in Run BASIC are indexed beginning at 0. An array dimensioned to 10 holds 11 elements, numbered 0 - 10.

Redim


An array can be redimensioned. This clears all array elements.

Syntax:

REDIM name(size1[, size2...]) - Redimension an already dimensioned array 
and clear all elements to zero (or to an empty string in the case of string arrays)

Assignment


Values are assigned to array elements with =. Here are some examples:

Toys$(1) = "Crayon"
Person$(1,0) = "Carl Gundel"
Person$(1,1) = "Run BASIC Creator"
total(20) = 145
total(21) = total(20) + 37

This small demo dimensions a string array and fills some of the elements by assigning text to them. It then prints out the contents in a loop.

dim Person$(10)
Person$(2) = "Carl Gundel"
Person$(1) = "Leonardo Da Vinci"
Person$(3) = "William Shakespeare"
for i = 1 to 3
print Person$(i)
next

The following demo dimensions an array with 3 dimensions, then fills it with some numbers. It prints out part of the third dimension.

dim myArray(10,10,10)
 
for i = 1 to 10
for j = 1 to 10
for k = 1 to 10
myArray(i,j,k) = i + j + k
next
next
next
 
for i = 1 to 10
print "MyArray element 1,1,";i;" is:"
print myArray(1,1,i)
next