|
||||||
Using Multidimensional Arrays with VBScriptHow to Work with More than One Dimension in VBScript ProgrammingA VBScript programmer can use multidimensional arrays in their applications. By using these they organize their data into lists, rectangles or even into a matrix
VBScript (or Visual Basic Script) multidimensional arrays enable a programmer to store data in complex matrices. By using multidimensional array a programmer can create a matrix of data rather than a list. However, before diving into the rather more complex world of VBScript multidimensional arrays it is probably best to consider a simple array first. A Simple VBScript ArraySimple VBScript arrays are discussed more fully in Windows Scripting: VBScript and Arrays: How to Create Multidimensional and Dynamic Arrays with VBScript, and they give an easy way of storing simple lists: Option Explicit
Dim Names: Names = Array ("Fred", "John", "Joan")
Dim x: For x = 0 to (ubound(Names))
wscript.echo Names(x)
Next
The output from this can be seen in figure 1 (at the bottom of this article), but arrays can be used for storing more than simple lists. A Two Dimensional ArrayIf a simple VBScript array is analogous to a list then a two dimensional array is analogous to a grid of data (and is often referred to as a rectangular array). Here a 5 by 8 grid is defined: Dim WorkingWeek (4, 7)
WorkingWeek (0,0) = "Start working on Monday"
WorkingWeek (0,3) = "Remember to have lunch"
WorkingWeek (4,7) = "Stop working on Friday"
It's always worth remembering that an array starts with index number 0 and not 1. Bearing that in mind the contents of the array can now be accessed: Dim x, y
For y = 0 to ubound (WorkingWeek)
For x = 0 to ubound (WorkingWeek, 2)
If WorkingWeek (y,x) <> "" Then
wscript.echo WorkingWeek (y,x)
End If
Next
Next
It's worth noting the fact that the dimension number is used here with ubound in order to obtain the number of elements in the additional dimension, and the output from this can be seen in figure 2. A Three Dimensional ArrayIf a one dimensional array is analogous to a list, and a two dimensional array is analogous to a grid, then a three dimensional array is analogous to a cube (for example a Rubik's cube). The array elements are assigned in the same ways as with two dimensions (except, of course that there is an extra dimension): Dim cube (3, 3, 3)
cube (0,0,0) = "Top Left Front"
cube (1,1,1) = "Center"
cube (2,2,2) = "Bottom Right Back"
And the 3 dimensional array can be used in a similar manner to 2 dimensions: Dim x, y, z
For y = 0 to ubound (cube)
For x = 0 to ubound (cube, 2)
For z = 0 to ubound (cube, 3)
If cube (y,x,z) <> "" Then
wscript.echo y, x, z, cube (y,x,z)
End If
Next
Next
Next
The result of using this code can be seen in figure 3. Nth Dimensional ArraysIt is possible to have a maximum of 32 dimensions in a VBScript multidimensional array, but more than that will cause VBScript to run out of memory. However, since that's more than enough dimensions for a Quantum physicist then it's probably enough for the average VBScript programmer.
The copyright of the article Using Multidimensional Arrays with VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish Using Multidimensional Arrays with VBScript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||