Creating Arrays

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The easiest way to create an array is to use the Array function. As easy as this is, however, there are two things to keep in mind when using the Array function. First, the Array function can be used only to create one-dimensional arrays. Second, this function requires you to know in advance each of the items to be placed in the array. In system administration tasks that usually is not the case: After all, one of the primary reasons you write a script is to determine these items (which services are installed on a computer, which computers are in a specified organizational unit, which printers are being managed by a particular print server. Only then could you place them in an array.

Nevertheless, at times you will know the items in advance, and thus will be able to use the Array function. For example, the script used in the first half of this chapter assumed that you needed to run the script against exactly three computers. This might occur in your organization as well: If you have eight e-mail servers, you might want to hard-code all eight computer names into a script.

To put items in an array, use the Array function. With this function, you simply assign a list of values to a variable. For example, the following code statement assigns four domain controllers to an array variable named Computers:

Computers = Array("atl-dc-01", "atl-dc-02", "atl-dc-03", "atl-dc-04") 

Declaring and Populating Arrays

Of course, there is a very good chance that you will not know, in advance, the items to be stored in an array. For example, your script might read in computer names from a text file, or might populate an array using the names of files found in a folder. In either case, you will not know the actual array items, or even how many items there are, until the script is run.

VBScript allows you to designate a variable as being an array without using the Array function and without immediately populating that array. To do this, you must use a Dim statement and must indicate the maximum number of items the array can hold. This can be a little confusing because the dimension of the array is based on the highest allowed index number rather than the actual number of items. Because the first item in an array is actually item 0 rather than item 1, the maximum number used in the Dim statement must always be the maximum number minus 1. If your array will contain 9 items, the Dim statement must show the maximum number as 8 (9   1).

For example, this command creates an array variable named arrTestArray and indicates that the array can contain a maximum of 4 items (4 1 = 3):

Dim arrTestArray(3) 

After you have declared an array, you can populate it by assigning a value to the appropriate item. For example, this line of code assigns the value "A" to item 0, the first item in the array:

arrTestArray(0) = "A" 

This simple script creates an array named arrTestArray and assigns values to each of the four items:

Dim arrTestArray(3) arrTestArray(0) = "A" arrTestArray(1) = "B" arrTestArray(2) = "C" arrTestArray(3) = "D" 

So far, so good. But what if you guessed wrong when originally declaring the array? What if arrTestArray will actually contain five items rather than four? If that is the case, an error will be generated when you try to assign a fifth item to the array; an array cannot contain more items than the maximum number assigned to it. For example, this sample script creates a four-item array and then attempts to assign a value to a fifth item:

Dim arrTestArray(3) arrTestArray(0) = "A" arrTestArray(1) = "B" arrTestArray(2) = "C" arrTestArray(3) = "D" arrTestArray(4) = "E" 

When the preceding script runs, a "Subscript out of range" error is generated.

In a situation such as this, you have two options:

  • You can allocate more space to the array than you will need. For example, you might declare, in advance, that arrTestArray will contain a maximum of 1,000 items. That provides adequate storage space for the array. However, it also wastes memory, and hundreds of items in the array might have no value at all.
  • You can create a dynamic array.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net