Copyright 2008 Shoptalk Systems
DIM array(size[, size2...])
DIM array(size) -or- DIM array(size1, size2) -or- DIM array(size1, size2, size3), etc...
DIM array1(size1), array2(size2), array3(size3), etc.
DIM sets the maximum size of an array. Any array can be dimensioned to have as many elements as memory allows. The DIM statement can be followed by a list of arrays to be dimensioned, separated by commas.
Note: For compatibility with older BASIC implementations, you can get away with not dimensioning an array, but it will only have a default size of eleven elements from 0 to 10. Best practice is to always use DIM.
'Example 1 - do not use DIM
print "Please enter 10 names."
for index = 0 to 10
name$(index) = name$
'Dimension three arrays at once
dim arrayOne(100), arrayTwo$(100, 5), arrayThree(100, 100)
The FOR . . . NEXT loop in the example above is limited to a maximum value of 10 because the array names$( ) is not dimensioned, and therefore is limited to 11 elements, numbered 0 - 10. To remedy this problem, add a DIM statement, as in the example below. Notice that it is not necessary to use all available index numbers. In the example the array is filled beginning at index 1, ignoring index 0.
print "Please enter 20 names."
for index = 1 to 20
names$(index) = name$
Double subscripted arrays can store information more flexibly:
dim customerInfo$(10, 5)
print "Please enter information for 10 customers."
for index = 0 to 9
input "Customer name >"; info$
customerInfo$(index, 0) = info$
input "Address >"; info$
customerInfo$(index, 1) = info$
input "City >"; info$
customerInfo$(index, 2) = info$
input "State >"; info$
customerInfo$(index, 3) = info$
input "Zip >"; info$
customerInfo$(index, 4) = info$
Note: You are not restricted to 2 dimensions. Keep in mind that each dimension that you add multiplies memory consumption exponentially. You can run out of memory very easily if you decide you need 3 or more dimensions in an array.