2.6. Array Variables

[<<<] [>>>]

ScriptBasic can handle arrays. Practically any variable can be an array, just like interger, real or string. Array is a variable type when a variable holds not only one, but many values and these values can be accessed using the variable name and an index value. In ScriptBasic arrays are automatically created. There is no statement like DIM. All you have to do is to use the variable like an array. Array subscripts are written using square brackets. This is usually the convention in PASCAL and in C. Other BASIC languages use normal parentheses to index arrays. However that confuses the reader as well as the parser, because function call looks the same as array access. Therefore ScriptBasic uses the square brackets. There is no limit on the number of indices. You can use many as you like. Also there is no limit on the index values. Positive, negative or zero integers can play the role of an index. Whenever you access an array element or assign a value to that ScriptBasic automatically checks if the referenced array element exists or not and adjusts the array if necessary. For example:

Example array1.bas :

a[1] = 3
for i=1 to 5
print a[i]

Result executing array1.bas :

Arrays are handled quite liberal. You are not required to declare the index bounds, you need not declare the number of indices. As a matter of fact you can have different number of indices at different points in the array. For example the following code is also legal:

a[1] = 3
print a[1],"\n",a[5,3]

You can even write:

a[1] = 3
print a[1],"\n",a[5,3,1,6,5,4,3,6,4,3,2222]

if you wish. But what happens if you write:

Example array2.bas :

a[1] = 3
print a[1],"\n",a[5]

Result executing array2.bas :

or some similar message. What has happened? To understand we have to explain how ScriptBasic stores the arrays. An array in ScriptBasic is stored as a list of C pointers. When a ScriptBasic variable first time used as an array a new array of a single element is created. It has one element assigned to the index that was referenced.

VARIABLE[6] = 555

Later, when other elements are referenced the array is automatically extended. For example if the array was first time referenced using the index 6 and it was accessed second time using the index 11 ScriptBasic automatically extends the array to contain six elements, the elements indexed with the values 6,7,8,9,10 and 11.

VARIABLE[11] = "a string"

This means that an array can consume significant memory even if only a few indices are used. When an array element, letís say index 10 is used with a second index, letís say with 3 a new

VARIABLE[10,3] =6.24E3

one-element array is created. Later this new array is treated the same as the one assigned to the variable itself, and when the element

VARIABLE[10,6] = 55

is set it is enlarged to be able to hold all values between and including indices 3 and 6. When the variable in our example named VARIABLE gets a new value, like


the arrays are released automatically. When accessing an array element, which is an array itself ScriptBasic tries to do its best. However the result may not be what you expect. The following program:

Example array3.bas :

c = a[5]
print a[5,3]," ",c

Result executing array3.bas :

in current version of ScriptBasic, but this may change later. That is because the expression a[5] that we try to assign to the variable c is an array itself. However ScriptBasic does not support the assignment of whole arrays. Therefore ScriptBasic tries to calculate the integer, double or string value that the programmer wanted to assign to c. In other BASIC implementations you have to use the declaration DIM and after that you have the array and you know the index limits of the array. In ScriptBasic there is no limit on the indices. Somehow you still may want to know the smallest and the largest index values used. For the purpose there are two functions named lbound and ubound. For example

Example array4.bas :

a[1] = undef
print lbound(a)," ",ubound(a)
a[2,-3] = "whoops"
print lbound(a)," ",ubound(a)
print lbound(a[2])," ",ubound(a[2])

Result executing array4.bas :

As you can see the argument to these functions can be a variable that has array value or an expression that has an array value. (Such an expression is likely to be an array element, which is an array by itself.)

[<<<] [>>>]