Arrays
This article is a stub. You can help us improve thinBasic Wiki Complete by expanding it.

Arrays are data structures that can contain more than one piece of data of one data type.

Creating arrays of variables

For creation of arrays apply the same conditions as for standard variables, you just have to specify number of dimensions and elements.

To create single dimension array of 100 elements, the following samples are valid

DIM    myArray(100)  AS LONG  ' -- Array of long integer variable, 100 elements indexed 1 ... 100
LOCAL  myArray(100)  AS LONG  ' -- Array of long integer variable, 100 elements indexed 1 ... 100, available as LOCAL
GLOBAL myArray(100)  AS LONG  ' -- Array of long integer variable, 100 elements indexed 1 ... 100, available from anywhere

Unlike some other programming languages, the arrays are "1" based.

In case you write to element, which is out of bounds, run time error window will pop up, with description of problem, passed wrong index as well as hint for valid array indexes.
You can turn off this checking using:

#DEFAULT BOUNDCHECK OFF

Multidimensional arrays

thinBASIC supports up to 3 dimensions for an array. To declare number of elements array has in each dimension, just separate numbers using comma.

<arrayName>(elements1[, elements2, [elements3]])

To declare array 8x8 elements you can use following statement:

DIM chessBoard(8, 8) AS LONG

How to assign value to an array

To demonstrate this technique, we will presume following array as our test example:

DIM testArray(5) AS LONG = 1
' -- or also
DIM testArray(5) AS LONG VALUE 1

And with the variable declaration we can assign to array already!
The code above will initialize all elements of the array to number "1".

If we would like to assign values 1, 2, 3, 4, 5 to the array, we can do it in multiple ways.
Here are few of them:

Way #1

testArray(1) = 1
testArray(2) = 2
testArray(3) = 3
testArray(4) = 4
testArray(5) = 5

Clear, but also with lot of typing.

Way #2

ARRAY ASSIGN testArray = 1, 2, 3, 4, 5
' -- or
ARRAY ASSIGN testArray() = 1, 2, 3, 4, 5

Now this is something different, it results in the same effect as #1, but using specific keyword

Way #3

testArray(1) = 1, 2, 3, 4, 5

The compact way. thinBASIC will again do the same as it would in example #1.
This syntax is most flexible, as if we would want to fill indexes 3 - 5 with 30, 40, 50 we could do:
testArray(3) = 30, 40, 50

So the index in brackets is considered the first one, and with each comma it "grows up".

Functions and commands operating on the arrays

thinBASIC offers following functions to make work with arrays easier:

ARRAY ASSIGN - as mentioned above, can serve to assign multiple values to an array
ARRAY SCAN - seeks for specified element in numeric or string array and returns its index
ARRAY SORT - sorts contents of numeric or string array in ascendant/descendant order
ARRAY SUM - sums all elements of numeric or string array
JOIN$ - returns string containing list of values of given array, separated using defined separator
PARSE - converts string to array, by decomposing it according to specified separators ( inverse of JOIN$ )
RESET - resets numeric array to zeros, string array to empty strings

REDIM - changes number of elements array can have, and erases all its content
REDIM PRESERVE - changes number of elements array can have, and preserves all its content

Related Pages

Backlinks

These pages link back to this page. You may find them interesting.

Links

Ask a Question

Ask a question about arrays.

page_revision: 4, last_edited: 1206214847|%e %b %Y, %H:%M %Z (%O ago)