Arrays
An array can be thought of as a group of things (usually variables) all organized under a single name. The method of organization works much like a matrix. Here are two examples of matrices:
0 1 2 0,0 0,1 1,0 1,1
The first is a 1x3 matrix; there is one column of data, with three possible entries. The second is a 2x2 matrix; there are two columns with two entries each. Matrices can have up to several hundred or thousand rows and columns, if you so desire, but most people will not have a need for that many when using arrays in OML. The numbers inside of the parentheses work like coordinates on the map; go across the number of slots indicated by the first number, then down the number of slots indicated by the second number, and put a piece of information there, or come back with whatever piece of information is there.
Rather than matrices' "columns" and "rows", arrays are usually thought of as having "dimensions" and "elements". (This is in part because arrays are not usually written out on paper, and can also handle more than three dimensions and three elements, making the map analogy less than accurate in some cases.) All arrays must be defined before use:
Dim Array1(2, 1 to 3, 2)
This example creates an array with three dimensions, each of which can have up to three elements. The elements for the first and third dimensions are numbered 0, 1, and 2--because 0 is the default starting point--and the elements for the second are numbered 1, 2, and 3.
As with variables, arrays can be defined as containing a certain type of data, or they can be left unspecified and the computer will figure out what you want as you go. However, once an element has had a certain type of data stored in it, be careful that you don't mix up your data types and try to store a different type of data there or try to add together different data types.
Dim Array1(3) as Integer
This example creates an array with one dimension and four elements, each of which must be an integer.
Here is an example of the use of an array. It creates an array with two dimensions, then uses the first dimension for numbers and the second for strings. Watch what the macro then is able to do with the information stored in the array.
Sub Main Dim MyArray(1,1) MyArray(0,0) = 1 MyArray(0,1) = "apple" MyArray(1,0) = 2 MyArray(1,1) = "orange" For i = 0 to 1 For j = 0 to 1 If j = 0 then PutLine = MyArray(i,j) Else OutputData$ = OutputData$ & CStr(PutLine) & " " & MyArray(i,j) & Chr(10) End if Next j Next i MsgBox OutputData$ End Sub
The end result of this macro is a screen that looks like this:
1 apple 2 orange
Watch the flow of commands and program logic of the second half of that program. What it does is start with i = 0 and j = 0. Since j = 0, it sets up the PutLine variable. It then increments j, so that j = 1, and goes back through the commands between the innermost For and Next. Since j = 1, it changes the value of OutputData$, using the PutLine data that was set up and the information stored in the second dimension of the array. (The "Chr(10)" on the end of OutputData$ will create a line break when the data is eventually displayed in a message box.) Since j has now reached the maximum it can get, the program goes on to the next line, which increments i, so that i = 1. Going to the next program line, j is redefined as 0 (because the For j... command was reached from a command other than "Next j") and the inner set of commands executes again. If you couldn't follow that, here are some visual cues to what the program is doing. It may help to print out a copy of the above program and point to each line or set of commands as you go through each line of the following:
Start program
Set up an array named MyArray, then fill it with data as follows:
MyArray | 0 | 1 |
0 | 1 | apple |
1 | 2 | orange |
i = 0, j = 0
Since j = 0, PutLine = a(0,0) = 1
i = 0, j = 1
Since j = 1, OutputData$ = "1" & " " & "apple" & Chr(10)
i = 1, j = 0
Since j = 0, PutLine = a(1,0) = 2
i = 1, j = 1
Since j = 1, OutputData$ = "2" & " " & "orange" & Chr(10)
End program
Arrays can be useful when you want to go through a series of variables, one right after the other, without needing a huge chunk of program dedicated just to setting up each individual variable, and without particularly needing individual mnemonics for each variable. Here is a simple example of this:
Sub Main Dim SomeArray(1 to 4) For i = 1 to 4 SomeArray(i) = InputBox("Enter number #" & i & " of 4:") Next OutputData$ = "The numbers you entered were:" For j = 1 to 4 OutputData$ = OutputData$ & " " & SomeArray(j) Next MsgBox OutputData$ End Sub
Note that many of the functions of one-dimensional arrays can be duplicated via adding all of the data together into a single string, using some character or character combination as a separator, and then using the GetField command to retrieve whichever piece of data you desire. The above example could be rewritten like this:
Sub Main InData$ = InputBox("Enter four numbers, separated by | (e.g. 1|2|3|4)") OutputData$ = "The numbers you entered were:" For i = 1 to 4 TempData$ = GetField(InData$, i, "|") OutputData$ = OutputData$ & " " & TempData$ Next MsgBox OutputData$ End Sub
(As you could probably figure out from that example, there is a second, very different, GetField command out there, this one without the "CS." prefix. This one takes in a string containing data separated by some sort of delimiter character, which field in that string to return, and a string specifying the separator character. So if the source string is "a, b, c, d" and you specify a comma as the separator character and ask it for field #3, it will return " c". Since it's easy to confuse these commands, always double-check your code to make sure you've used the right one!)
Which of those methods you would use depends on several factors, including which would use fewer lines of code in your application, which is more stable in your application, and which you find easier to use. As with many things in life, there are situations in which it is good to use an array and situations in which it is not. Unlike with life, however, it is often fairly obvious which is which with arrays.
Next time, error handling...