Arrays
Jump to navigation
Jump to search
Description
- Arrays are collections of indexed variables, which are defined as either string or numeric. They are most often used to store a list of related items, with any item's value being retrievable by referring to its index number. A clear example would be a string array of the days of the week, numbered weekDay$(1) thru weekDay$(7).
Syntax
Numeric array:
- arrayName(indexNumber)=numeric value
String array:
- arrayName$(indexNumber)="string value"
Hints
- Dimensioning Arrays. An array that will hold more than 11 items (indexed 0 to 10), must be dimensioned with the DIM statement. Example, single dimensioned string array:
Dim monthOfYear$(12)
- JB and LB4 support single and double-dimensioned arrays.
- RB supports arrays with up to 5 dimensions, but all arrays need to be dimensioned before use
- Double-dimensioned array with at least one dimension exceeding 10 must be DIM'ed before use. Example, double dimensioned numeric array:
Dim dayOfMonth(12, 31)
- You must dimension an array large enough to hold all the items or you will get a runtime error: "Subscript out of range" if you try to access an item with an index number larger than the dimension number.
- Indexing arrays.
- Arrays always include 0-th item. That is, DIM a(15) actually makes a space for a(0), a(1), ... a(15) - total 16 items.
- It allows you to use arrays from item 0, like then porting code from C language. Or you can ignore 0-th item and use array from item 1.
- Arrays in a SUB/FUNCTION.
- Arrays are always global in LB4 and JB, that is visible in any SUB or FUNCTION
- For RB arrays are only global, if they are dimensioned in the main program scope, arrays dimensioned in a procedure are local in scope
- There are no way to pass an array into SUB or FUNCTION (that is, you cannot create a single procedure and use it on several arrays)
- Passing array elements ByRef does not work (changes do not propagate back)
- Using Arrays in Loops. Because an array is a collection (a structure in a sense), it is possible to obtain the value of all the items by using a FOR...NEXT loop, where each time through the loop the index number is incremented and points to the next item in the array. For example, to print all the days of the week, you might do this:
'assume the array holds the values "Monday", "Tuesday", etc. for day = 1 to 7 print weekDay$(day) next day
- Assigning Array Values. To "hard-code" in the values for an array, you use assignment statements. For example, to create an array that holds the names of all the days of the week, you could do this:
weekDay$(1) = "Monday" weekDay$(2) = "Tuesday" weekDay$(3) = "Wednesday" weekDay$(4) = "Thursday" weekDay$(5) = "Friday" weekDay$(6) = "Saturday" weekDay$(7) = "Sunday"
- For Combo Boxes and List Boxes the following suggestion should be followed to keep the array index in sync with the display.
- Incidentally, even though an array begins with index 0, it is best not to use index 0 and instead make your first item index 1. Any index that does not have a value assigned to it will be ignored.
- Using Data Input to Fill an Array. You can read data from a data file directly into an array. See INPUT.
'create file open "test.dat" for output as #1 print #1, 101 print #1, 202 print #1, 303 close #1 'input to array dim a(3) open "test.dat" for input as #1 for i = 1 to 3 input #1, a(i) next close #1 'check for i = 1 to 3: print a(i): next
'reading array from DATA statement data 101, 202, 303 'read to array dim a(3) for i = 1 to 3 ' read a(i) 'will not compile: In Just BASIC, data must first be read into a variable before read tmp a(i)=tmp next 'check for i = 1 to 3: print a(i): next
Example
'dimension the array dim weekDay$(7) 'assign values to the array elements weekDay$(1) = "Monday" weekDay$(2) = "Tuesday" weekDay$(3) = "Wednesday" weekDay$(4) = "Thursday" weekDay$(5) = "Friday" weekDay$(6) = "Saturday" weekDay$(7) = "Sunday" open "Array Demo" for window as #1 #1 "trapclose [quit]" 'results will print to mainwin for i = 1 to 7 print weekDay$(i) next wait [quit] close #1 end
Useful Procedures
' Place a useful function using this keyword here