HTBasic Help
×
Menu
Index

Array Variables

 
A simple variable has one data value. An array is a multi-dimensional ordered set of data values. Each member of the set is called an array element. All the members of the set have the same data type which can be COMPLEX, INTEGER, LONG, REAL, or String. An array variable can not have the same name as a simple variable.
 
The number of dimensions of the array is called the Rank. Arrays may have a Rank from one to six. You can specify both the lower and upper bound of each dimension. If the lower bound is not specified then the current OPTION BASE of the context is used as the lower bound. The default OPTION BASE is zero.
 
Local array variables are declared using the COMPLEX, INTEGER, LONG, REAL, STATIC and DIM statements.
ALLOCATE is used to dynamically declare a local array.
The COM statement is used to declare a global array.
 
Normally, all array variables that are not explicitly declared will have a default lower bound, an upper bound of 10, and a Rank matching the number of subscripts in the first reference of the array. To disable automatic array declaration, use CONFIGURE DIM OFF.
 
A string array may be defined where each element of the array is a separate string of the dimensioned length. To dimension a string array named S$ with four elements (assuming the default OPTION BASE 0), each with a maximum length of 20 characters::
 
DIM S$(3)[20] ! 4 array elements (0-3) of 20 chars max length each
 
 
The following defines an array with elements numbered 0,1,2,3 (for OPTION BASE 0),  or 1,2,3 for OPTION BASE to 1:
 
DIM X(3)           ! declares an array of 3 or 4 REALs.
 
INTEGER A(50:100)   ! declares an array of 51 integers.
 
One-dimensional arrays are always referenced by one subscript in parentheses following the array variable. For example, A(2) refers to the third element. Two-dimensional arrays are referenced by two subscripts, where the first subscript refers to the row, and the second subscript refers to the column. For example, A(1,2) refers to the second row, the third column. An element of an array can be used wherever a simple variable of the same type can be used.
 
Here's a 2x2 array assignment:
 
DIM A(1:2,1:2)
A(1,1) = 5
A(1,2) = 6
A(2,1) = 7
A(2,2) = 8
 
READ from DATA can assign array values more economically (in fewer lines). For example:
 
DIM A(1:3,1:4) ! 3x4
DATA 5,6,7,8,9,10,11,12,13,14,15,16
READ A(*)      ! gets all 12 values
 
MAT (matrix) can define an entire array. For example:
 
DIM A(100,100)
MAT A = (1)    ! initializes all 10,000 elements in A to 1
 
You can also assign array values with the INPUT statement:
 
DIM A(1:2,1:2) ! A is dimensioned as a 2 by 2 array
INPUT A(*)     ! gets values from the keyboard, (i.e. 5,6,7,8)
 
You can also use a FOR/NEXT loop to assign values in some other order or starting point. For example:
 
DIM Beta(1:99)
FOR J = 40 TO 50
  ENTER @Path;Beta(J)
NEXT J