Previous Page
Next Page

Working with Arrays

Because we have discussed collections, you might find it easy at this point to think of arrays as collections that you create and can controland you would be right. Arrays are like collections you can create and control yourself. There are several nice aspects of arrays; for example, you can populate them with information for later use in the script. In addition, you can create an array dynamically during the execution of the script. You'll explore each of these concepts in this section.

Just the Steps

To create an array

1.
On a new line, use the Dim command to declare the name to use for the array.

2.
Populate the array by assigning values to the name declared in the first line by using the Array command and enclosing the values in parentheses.


One way to create an array is to use the Dim command to declare a regular, or normal, variable. You then use the variable to populate the array with computer names and use a For Each...Next loop to walk through the array. Remember, an array is basically a collection, and you therefore need to use a For Each...Next loop to walk through it. The following script creates an array with the names of three computers. The variable i is used as a counter to track your progress through the collection. Because an array is zero-based (that is, it begins counting at zero), you set i to an initial value of zero. Next, you populate the array with your computer names, making sure to enclose the names in quotation marks; and you use a comma to separate the values. The collection of computer names is placed inside the parentheses. You use a For Each...Next loop to walk through and echo the computer names to the screen. You then increment the counter i to the next number and go back into the For Each...Next loop. Because For Each...Next already knows how to retrieve an item from a collection, we do not need to point to a specific element in the array. Using For Each...Next in this manner is a great way to walk through an array when you do not know how many items are in the array. This script, BasicArrayForEachNext.vbs, follows.

BasicArrayForEachNext.vbs

Option Explicit
On Error Resume Next
Dim myTab 'Holds custom tab of two places
Dim aryComputer 'Holds array of computer names
Dim computer    'Individual computer from the array
Dim I           'Simple counter variable. Used to retrieve by
                'Element number in the array.
myTab = Space(2)
i = 0           'The first element in an array is 0.
aryComputer = array("s1","s2","s3")

WScript.Echo "Retrieve via for each next"
For Each computer In aryComputer
         WScript.Echo myTab & "computer # " & i & _
         " is " & computer
   i = i+1
Next

Another approach to dealing with elements in an array is to use the For...Next statement. As you may recall from Chapter 2, For...Next enables you to walk through a collection if you know how many times you want to do something. Because you may not always know how many items you have in the array, it is helpful to use the function UBound. UBound acts just like the Count method we used with the command-line arguments. It tells us how many items are in the array. Armed with this information, we can use For...Next. This is seen in the BasicArrayForNext.vbs script below.

BasicArrayForNext.vbs

Option Explicit
On Error Resume Next
Dim myTab 'Holds custom tab of two places
Dim aryComputer 'Holds array of computer names
Dim computer    'Individual computer from the array
Dim i           'Simple counter variable. Used to retrieve by
                'Element number in the array.
myTab = Space(2)
i = 0           'The first element in an array is 0.
aryComputer = array("s1","s2","s3")


WScript.Echo "Retrieve via for next"
i = 0
For i = 0 To UBound(aryComputer)
   WScript.Echo myTab & "computer # " & i & _
         " is " & aryComputer(i)
Next


Previous Page
Next Page