Individual array elements are selected with subscripts or index numbers, which are Long-integer expressions within parentheses to the right of an array variable's name. For example, payments(3) and payments(44) are two of payments 56 elements. Normally, the first element of an array has a subscript value of zero, although this can be changed with the DIM statement. Some examples follow:
' This DIM statement declares a 56-element array
' with subscript bounds of 0 TO 55.
DIM payments1(55) AS CURRENCY
' This DIM statement declares a 56-element array
' with subscript bounds of 1 TO 56
DIM payments2(1 TO 56) AS CURRENCYX
You must DIM all arrays before you can use them. This is a different approach then that used by some BASIC dialects, which assume that an array contains 10 elements (0 to 9) if the array is not explicitly dimensioned.
PowerBASIC allows you to define a range of subscript values rather than just setting an upper limit. In addition to the TO syntax shown above, PowerBASIC supports the classic BASIC syntax that uses a colon (:) character to separate range values. Use the TO syntax in preference to the colon syntax, since the latter may not be supported in future versions of PowerBASIC. For example, the following two DIM statements are equivalent:
DIM fog(50:60) AS LONG ' Classic syntax
…or
DIM fog(50 TO 60) AS LONG ' Preferred syntax
…and both work to create an array fog, consisting of eleven Long-integer elements numbered 50 through 60.The statement:
DIM clouds(50 TO 60, 25 TO 45) AS LONG
creates the two-dimensional Long-integer array named clouds, containing 231 (11 * 21) elements. PowerBASIC's subscript range declaration capability allows you to model a programs data structures more closely to the problem at hand.
For example, consider a program tracking 19th-century birth statistics. This program's central data structure is a Long-Integer array of 100 elements that contain the number of babies born in each year of the last century. Ideally, you would create an array that used subscript values equal to the year in which the births occurred (for example, births(1851) represents how many babies came into the world in 1851), so that a code passage like:
DIM births(1899) AS LONG
FOR year& = 1800 TO 1899
INCR Total&, births(year&)
NEXT year&
would be as straightforward as possible. Unfortunately, DIM births(1899) AS LONG creates a 1900-element array (from 0 to 1899), of which the first 1800 are wasted. Traditionally, BASIC programmers have tackled this problem by declaring the array as:
DIM births&(99)
and by playing games with subscripts:
FOR year& = 1800 TO 1899
INCR Total&, births&(year&-1800)
NEXT year&
While this sort of thing works, it complicates things and slows programs down because suddenly there are 100 subtractions that weren't there before. It's better to declare a range, like this:
DIM births&(1800 TO 1899)' array births has subscripts
' ranging from 1800 to 1899
FOR year& = 1800 TO 1899
Total& = Total& + births&(year&)
NEXT year&
DIM birth1&(99) ' Array has 100 elements from 0 TO 99
DIM birth2&(1 TO 99) ' Array has 99 elements from 1 TO 99
DIM birth3&(3 TO 99) ' Array has 97 elements from 3 TO 99
See Also
Internal representations of arrays
Arrays within User-Defined Types