BB___Arrays

=Arrays= toc

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 refering 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: String array:

Hints
>> code format="lb" Dim monthOfYear$(12) code > JB supports single and double-dimensioned arrays. > Double-dimensioned array with at least one dimension exceeding 10 must be DIM'ed before use. Example, double dimensioned numeric array: >> code format="lb" Dim dayOfMonth(12, 31) code > 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. > 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. >> code format="lb" 'assume the array holds the values "Monday", "Tuesday", etc. for day = 1 to 7 print weekDay$(day) next day code >> code format="lb" weekDay$(1) = "Monday" weekDay$(2) = "Tuesday" weekDay$(3) = "Wednesday" weekDay$(4) = "Thursday" weekDay$(5) = "Friday" weekDay$(6) = "Saturday" weekDay$(7) = "Sunday" code > 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 by JB. >> code format="lb" 'create file open "test.dat" for output as #1 print #1, 101 print #1, 202 print #1, 303 close #1
 * //** 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:
 * //** Indexing arrays.**//
 * //** Arrays in a SUB/FUNCTION.**//
 * 1) Arrays in JB are global, that is visible in any SUB or FUNCTION
 * 2) 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)
 * 3) 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:
 * //**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:
 * //**Using Data Input to Fill an Array.**// You can read data from a data file directly into an array. See INPUT.

'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 code > You can also use READ statements to fill an array with DATA. >> code format="lb" '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 code

Example
code format="lb" '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 code

Useful Procedures
code format="lb" Place a useful function using this keyword here code