Scalar and Array Variables

In most cases, once you have declared a variable, you will only assign it a single value. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. You can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name.

In the following example, a single-dimension array containing 11 elements is declared:

Dim A(10)

Although the number shown in the parentheses is 10, all arrays in BSL are zero-base. So, this array contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.

You can assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows:

	A(0) = 256
	A(1) = 324
	A(2) = 100
	 . . .
	A(10) = 55

Similarly, the data can be retrieved from any element using an index into the array element you want.

For example:

SomeVariable = A(8)

Arrays aren't limited to a single dimension. You can declare multiple dimensions by separating an array's size numbers in the parentheses with commas. In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5,10)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.

You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the ReDim statement. However, for a dynamic array, is placed no size or number of dimensions inside the parentheses.

For example:

Dim MyArray()
ReDim AnotherArray() ' Illegal as no dimension specified
ReDim AnotherArray(3)

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30 but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)
 . . . 
ReDim Preserve MyArray(30)

There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.

The following examples illustrates the use of the Scalar variables and array variables:

Example 1:

Dim B
B = 200   ' Assigns the value 200 to the variable B.

Example 2:

Dim A(3)   ' Declares a single-dimension array with 4 elements 0 to 3 indexes.
' Assigning values to each element of the array.
A(0) = 256
A(1) = 324
A(2) = 100
' Retrieving a value from the array.
Dim SomeVariable
SomeVariable = A(1)   ' Retrieves the value at index 1.

Example 3:

Dim MyArray()   ' Declares a dynamic array.
ReDim MyArray(25)   ' Sets the initial size of the dynamic array to 25 elements.
' Assigning values to the dynamic array.
MyArray(0) = "First"
MyArray(25) = "Last"
' Resizing the dynamic array and preserving contents.
ReDim Preserve MyArray(30)   ' Resizes the array to 30 elements and preserves existing data.

Example 4:

Dim AnotherArray()
ReDim AnotherArray(10)   ' Initial size of 10 elements.
' Assigning values to the array.
AnotherArray(0) = "Alpha"
AnotherArray(10) = "Omega"

' Resizing the array multiple times.
ReDim Preserve AnotherArray(20)
AnotherArray(20) = "NewElement"
ReDim Preserve AnotherArray(5)   ' Reduces the size of the array (data beyond index 5 is lost).