Arrays

Arrays, Dim and Redim
toc 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: code DIM name(size1[, size2...]) - Dimension an array of one or more dimensions of size code

Some code examples using **Dim**

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

Arrays can be dimensioned using a variable for the value.

code total = 45 Dim Nums(total) code

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:

code 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) code

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

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

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.

code format="vbnet" 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 code

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

code format="vbnet" 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

code