Working With Arrays in VBScript - Dynamic and Multidimensional Arrays
(Page 5 of 5 )
VBScript also allows you to create more advanced types of arrays: arrays whose sizes can be changed dynamically and arrays with multiple sets of elements. These types of arrays allow you to process groups of data or dynamic data more easily.
A dynamic array is really not a new type of array. It’s simply an array whose size can be changed dynamically. This is accomplished through the use of the ReDim statement.
arrNames = Array()
ReDim arrNames(2)
arrNames(0) = "John Doe"
arrNames(1) = "Jane Smith"
arrNames(2) = "Dick Tracy"
In this example, I’m using the Array function to create an empty dynamic array. Then I use the ReDim statement to resize the array before assigning values.
ReDim Preserve arrNames(3)
arrNames(3) = "Nilpo"
The ReDim statement will empty all elements in an array while resizing it. You can prevent this by using the Preserve keyword. If the size is being increased, it simply adds empty elements. When decreasing, it will retain any elements that fit inside the new dimension and discard the rest.
This can be extended to create a very powerful mechanism for creating arrays dynamically. For example, I could read a text file and assign each line of that file to an array element. Since I may or may not know how many lines are in the text file, it’s quite handy to do this dynamically.
Const ForReading = 1
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("C:myfile.txt", ForReading)
arrLines = Array()
intCounter = 0
Do Until objFile.AtEndOfStream
ReDim Preserve arrLines(intCounter)
arrLines(intCounter) = objFile.ReadLine
intCounter = intCounter + 1
Loop
objFile.Close
You can see here where I’m reading each line of a text file into an array. I dynamically resize the array element (to increase its size by 1) before adding each new line. The Preserve keyword ensures that existing data remains intact through each resize, and the intCounter variable is incremented to track the number of elements in the array.
ReDim Preserve arrNames( UBound(arrNames) + 5 )
There may also be times when you wish to resize an array without knowing how many elements it contains. The example above adds five more elements to the arrNames array. I’ve used the UBound function to return the upper boundary of the array and add 5 to that number. The upper boundary represents the last element in the array.
Another advanced type of array is the multi-dimensional array. This is like an array of arrays—each element contains a group of sub-elements in much the same way as each row of a worksheet contains several other columns. For example, I could create an array of names. Each element of that array would represent a person’s name, but would contain sub-elements that contain their first and last names separately.
Dim arrNames(2, 1)
arrNames(0,0) = "John"
arrNames(0,1) = "Doe"
arrNames(1,0) = "Jane"
arrNames(1,1) = "Smith"
arrNames(2,0) = "Dick"
arrNames(2,1) = "Tracy"
The syntax for a multi-dimensional array is the same as any other array. You just add each additional dimension, separated by a comma. The arrNames array contains three parent elements. Each parent element contains two sub-elements as defined by the Dim statement. Each element can then be accessed by providing the proper dimensions. That’s why arrNames(2,1) represents the second sub-element of the third element in the arrNames array.
Dim arrNames(0)
arrNames(0) = Array("John", "Doe")
WScript.Echo arrNames(0,1) 'Does not work
WScript.Echo arrNames(0)(1) 'Does work
In this example, I’ve attempted to create a multi-dimensional array by assigning an array to another array element. This code works perfectly; however, I cannot access the sub-elements as I should be able to. Why? A multi-dimensional array is like an array of arrays—it’s not actually the same thing. This code adds an array reference to the first array, but does not actually add the second array’s data. This is still stored in memory in the second array. All I’ve really done here is nest two arrays.
As you can see, there are several ways to utilize arrays in VBScript. They can be very powerful mechanisms for accessing and processing data, but you have to be sure that they are created and handled properly. Go ahead and play around with these code samples. You’ll gain a better understanding through trial and error. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |