Previous Page
Next Page

Two-Dimensional Arrays

A two-dimensional array gives you the ability to store related information in much the same way you would store it in an Excel spreadsheet. To visualize a two-dimensional array, it is helpful to think of a spreadsheet that contains both rows and columns.

Just the Steps

To create a two-dimensional array

1.
On a new line, use the Dim command to declare the name to use for the array, followed by parentheses and the number of elements to be used for each dimension, separated by a comma.

2.
Populate the array by assigning values to the name declared in line 1 by using the array name and assigning a value with each element.


To create a two-dimensional array, include both dimensions when you declare the variable used for the array, as illustrated here:

Dim a (3,3)

All you've really done is include the extra dimension inside the parentheses. The array just listed contains two dimensions, each holding four elements for a total of 16 elements. Each dimension of the array is separated by a comma within the parentheses. Remember that the array begins numbering with a zero, and thus Dim a (3,3) states that the array a has four rows numbered from zero to 3, and four columns numbered from zero to 3.

The key points to remember about an array are that it resides in memory and can be used to hold information that will be used by the script. With a two-dimensional array, you have a matrix (not The Matrixbut a matrix nonetheless). Dim a (3,3) would look like the matrix in Table 4-2.

Table 4-2. Two-dimensional array

0,0

0,1

0,2

0,3

1,0

1,1

1,2

1,3

2,0

2,1

2,2

2,3

3,0

3,1

3,2

3,3


Each square in the array in Table 4-2 can hold a single piece of information. However, by using concatenation (putting strings together by using the ampersand) or by manipulating the string in other ways, you can get quite creative with the array.

Mechanics of Two-Dimensional Arrays

In the next script (WorkWith2DArray.vbs), a two-dimensional array is created. The script then populates each of the 16 elements with the string "Loop" concatenated with the loop number. In this way, you can keep track of where you are within the matrix as you echo out the value contained within the elements.

WorkWith2DArray.vbs

Option Explicit
Dim i
Dim j
Dim numLoop
Dim a (3,3)
numLoop = 0
For i = 0 To 3
  For j = 0 To 3
    numLoop = numLoop+1
    WScript.Echo "i = " & i & " j = " & j
    a(i, j) = "loop " & numLoop
    WScript.Echo "Value stored In a(i,j) is: " & a(i,j)
  Next
Next

Let's look at the script in a little more detail.

Header Information

The Header information section of the script follows the normal procedure of beginning with Option Explicit (which forces the declaration of each variable used in the script by using the Dim command). Next, two variables (i and j) are declared that will each be used to count from 0 to 3 within a For...Next construction. The variable numLoop is used to keep track of the 16 passes that are required to work through all 16 elements contained in the array. The last item in the Header information section of the WorkWith2DArray.vbs script specifically declares our two-dimensional array: Dim a (3,3).

Reference Information

The Reference information section of our script consists of one line: numLoop = 0. Because you use numLoop to keep track of how many loops are made through the array, it is important to set it to zero. Later, you'll reassign the value of numLoop to be equal to its current value in the loop plus 1. By incrementing the numLoop counter, you can easily know exactly where you are within the array.

Worker and Output Information

The Worker and Output information section of the script (shown in the next code listing) begins immediately with a pair of nested For...Next constructions. The reason for nesting the For...Next loop in this section of the script is to have a separate value for both the variable i and the variable j.

Using the For...Next Construction

Because the array was declared as Dim a (3,3) and you happen to know that the array is zero-based, you use i = 0 to 3 in the For...Next loop, as shown in the first line of the following script. You next increment the numLoop counter and echo the current values contained in the variables i and j. Once you know your location in the array, you assign the word loop concatenated with the current value held in the numLoop counter to the particular array element that is currently described by a(i,j). If, for instance, the script is in its first loop, the value of i is 0 and the value of j is 0, and when you get down to the WScript.Echo commands, the value of numLoop has already been incremented. So, you would echo "i = 0 j = 0". Look closely at the following script portion to make sure you understand what is happening in the first four lines:

For i = 0 To 3
  For j = 0 To 3
    numLoop = numLoop+1
    WScript.Echo "i = " & i & " j = " & j
    a(i, j) = "loop " & numLoop
    WScript.Echo "Value stored In a(i,j) is: " & a(i,j)
  Next
Next

Assigning Values to Each Element

Once the loop counter (numLoop) is incremented, it's time to assign a value to each element within the array. Rather than typing a whole series of a(0,0) = "loop" & numLoop lines, you instead dynamically build the value of a(i,j) by using the two For...Next loops. Thus, prior to assigning the value "loop" and numLoop to the array element, the element is empty.

Tip

To assign a value to an element within an array, you specify the element number, followed by the equal sign, and then specify the value. If, however, you use a For...Next loop, you can in many instances automate the process.


After you assign values to the array, you use one final WScript.Echo command to echo out the values that are contained within the array. This is where you'd do the actual work if this were a real script. You close out the script with a pair of Next commands: one for each For introduced earlier in the script.


Previous Page
Next Page