Do you have a scripting-related question or problem? You can send your question or problem to [email protected].
I'm writing a script in which I need to create and populate a VBScript 2-D array, then multiply each element in the array by a constant. However, I'm not sure how to proceed. Can you help?
The key to working with VBScript 2-D arrays is simple: Visualize the array as a table in memory. Just like a table, a 2-D array contains rows and columns. To identify the location of elements in an array, you use numbers or variables called subscripts. VBScript array subscripts are zero-based, so the following VBScript statement defines a 2-D array called Table that contains 10 rows (subscripts 0 through 9) and five columns (subscripts 0 through 4):
To store a value in a specific cell in the 2-D array, you use subscripts to specify the cell coordinates. For example, to store the value 0 in the cell at row 1, column 1, you'd specify
Table(0,0) = 0
Similarly, to store the value 49 in the cell at row 10, column 5, you'd specify
After you've initialized the array, you can easily multiply each cell by a constant with code such as
Const MULTIPLIER = 2 Table(0,0) = _ Table(0,0) * MULTIPLIER Table(9,4) = _ Table(9,4) * MULTIPLIER
Although this approach works, it isn't the most elegant because you'd need more than 100 lines of code to initialize and modify a 10 * 5 table. A better approach is to use nested For Each...Next statements (aka For loops), as the script 2dArray.vbs in Listing 1 shows. 2dArray.vbs begins by defining the MULTIPLIER constant (which will serve as the script's multiplier) and a handful of variables, including Row and Column. The script will use the Row and Column variables to reference individual cells inside the Table array. This 2-D array contains five rows and seven columns (35 cells total).
The code at callout A in Listing 1 uses two nested For loops to initialize the Table array. Nesting the loops lets you traverse the 2-D array much like you read a book: from left to right (columns) and from top to bottom (rows). The outer For loop in callout A iterates through the rows in the table, whereas the inner loop (i.e., the nested loop) iterates through the columns in each row.
The code at callout A begins by initializing the n variable to 0; n is a counter that will insert a unique numeric value in each cell of the Table array. Next, the outer loop sets the Row variable to 0, which corresponds to the first row in the Table array. The inner loop then sets the Column variable to 0, which corresponds to the Table array's first column. Combined, the two variables reference a unique cell in the Table array—that is, (0,0). The script assigns the (0,0) cell the current value of the n variable, then increments n by 1.
The UBound function in the inner loop tells the inner loop to continue iterating through the columns until the loop reaches the rightmost column. If you've used the VBScript UBound function for ordinary single-dimension arrays, this usage might look different. UBound supports an optional second parameter designed specifically for multidimensional arrays. The way to read the inner loop's use of UBound is as follows: Return when you reach the upper boundary of the second dimension (i.e., the columns) of the Table array. In this case, the upper boundary is 6, so the inner loop runs seven times (subscripts 0 through 6). After the seventh iteration, control returns to the outer loop, at which point the Row variable's value increments by 1. The outer loop then transfers control back to the inner loop, which iterates through the seven columns in the second row of the table. As the outer loop's UBound function dictates, this process continues until the outer loop iterates through all five rows (subscripts 0 through 4) in the Table array.
After the nested loops at callout A are finished executing, the Table array is fully initialized. Web Table 1 (http://www.winscriptingsolutions.com, InstantDoc ID 39313) shows the contents of the Table array at this point in the script.
The remainder of the script also uses nested For loops but for different purposes. The nested For loops at callout B in Listing 1 iterate through the Table array so that the script can read the contents of the newly initialized array. The script saves the array's contents in the string variable named strBefore. The additional decision logic inside the inner loop formats the string by adding one space before each single-digit value. The script sends, or echoes, the formatted string to the console at the end of the script.
The nested For loops at callout C in Listing 1 iterate through the Table array so that the script can multiply the numeric value in each table cell by the MULTIPLIER constant, which is defined at the top of the script. The script simply takes the current cell value, multiplies it by 2, and saves the result in the same cell.
The nested For loops at callout D in Listing 1 iterate through the Table array so that the script can read the contents of the updated array, which the script saves in the string variable named strAfter. The last two lines in the script echo the strBefore and strAfter variables' contents to the console. Figure 1 shows the results.