Previous Section Table of Contents Next Section

Working with Arrays

An array is a collection of values assigned to a single variable. Normal variables can hold just one value. For example:


Dim sMonths

sMonths = "January"

In this example, sMonths could be changed to contain "February," but doing so would eliminate "January" from the variable's contents. With an array, however, a single variable can contain multiple values. For example:


Dim sMonths(12)

sMonths(1) = "January"

sMonths(2) = "February"

sMonths(3) = "March"

sMonths(4) = "April"

sMonths(5) = "May"

sMonths(6) = "June"

sMonths(7) = "July"

sMonths(8) = "August"

sMonths(9) = "September"

sMonths(10) = "October"

sMonths(11) = "November"

sMonths(12) = "December"

This capability to assign multiple values to a single variable can come in handy in a number of scripting situations.

Arrays in VBScript

VBScript supports multidimensional arrays. For example, suppose you declare a variable using Dim sData(5,4). This creates a two-dimensional variable. The first dimension can hold six data elements, whereas the second dimension can hold five. Note that elements always begin numbering at zero. I sometimes find it easier to imagine a two-dimensional array as a table of elements. The columns represent one dimension, whereas the rows represent another dimension.

sData

0

1

2

3

4

0

Harold

Todd

Lura

Ben

Mary

1

Cyndi

David

Deb

Amy

Barb

2

Liza

Judy

Tina

Bette

Will

3

Martha

Doug

Peter

Derek

Jeremy

4

Don

Chris

Joe

Hector

Maria

5

Tom

Mary

Jill

Ruth

Bill

I might decide that the first dimension (the columns) represents different job positions at my company, such as Sales, Marketing, Human Resources, MIS, and Operations. I might decide that the second dimension represents individuals within each role. Therefore, sData(2,4) would contain "Joe," the fourth person in the MIS department; sData(0,1) would contain "Cyndi," the second person in the Marketing department; and so forth.

Three-dimensional arrays can be pictured as a cube, with each dimension of the cube (X, Y, and Z) representing a dimension of the array. Four-dimensional and larger arrays are a bit more difficult to imagine, but you get the idea; and fortunately, arrays larger than two dimensions are rare in administrative scripts.

Arrays are not actually a data type in and of themselves; they can, in fact, be any type of data I've shown you in this book: strings, numbers, bytes, dates and times, and so forth.

Creating and Manipulating Arrays

You can declare static arrays by using the Dim keyword, as I've already done in a couple of examples. You can declare a dynamic array by using the Dim keyword and by leaving one dimension of the array unspecified. For example, to declare a dynamic, single-dimension array, simply use Dim sVariable(). Notice that you still need to include the parentheses; these tell VBScript that you're declaring an array, but declining to specifically size it for now.

When you decide to size the array, you do so by using the ReDim statement. For example:


Dim sArray()

ReDim sArray(4)

This example will create a new array, and then size it to have five elements numbered zero to four. Note that ReDim() will remove any data in the array when resizing it. If you already have data in an array and want to keep it, add the Preserve keyword, as follows:


Dim sArray()

ReDim sArray(2)

sArray(0) = "One"

sArray(1) = "Two"

sArray(2) = "Three"

ReDim Preserve sArray(3)

sArray(3) = "Four"

The result of this example is an array of four elements, each containing string data. ReDim is pretty powerful.

  • You can use it to change the number of dimensions. For example, a one-dimensional array named sArray with four elements can be resized using ReDim sArray(4,2). Doing so adds a new dimension of three elements. However, you cannot use the Preserve keyword when changing the number of dimensions.

  • When you use the Preserve keyword, you can only resize the last dimension. For example, if you have a two-dimensional array named sArray, and already have four elements in each dimension, using ReDim Preserve sArray(8,4) would generate an error because you're trying to resize the first dimension in conjunction with the Preserve keyword.

  • You can resize an array to make it smaller. When you do, any data contained in the truncated portion of the array is lost.

You can also create arrays from an existing value. For example, suppose you have a script that's reading an IIS log file. Normally, those files are comma-delimited values. You might read an entire line of data into a variable named sLog, and that variable might contain something like, "12-12-2003,12:43,index.html,400" or something similar. If you want to get just the name of the Web page from that line of the log, you could use some heavy-duty string manipulation to find the third comma, pull out the Web page name, and so forth. However, because there's a comma delimiting each piece of data, it may be easier to convert the data to an array.


'sLog contains log file line

Dim sLogData()

sLogData = Split(sLog, ",")



MsgBox "Web page is " & sLogData(2)

The magic lies in the Split() function. This function accepts a variable, such as sLog, and a delimiter character, such as the comma. Split() returns an array, with one element for each piece of data separated by a comma. In my example, sLogData would contain four elements, numbered from zero to three. The third element, number two, contains "index.html," which is what I was after in the first place.

The opposite of Split() is Join(). This function accepts a one-dimensional array and a delimiter character, and returns a single delimited string. For example, using my sMonths array from the first part of this section:


Dim sMonths(12)

sMonths(1) = "January"

sMonths(2) = "February"

sMonths(3) = "March"

sMonths(4) = "April"

sMonths(5) = "May"

sMonths(6) = "June"

sMonths(7) = "July"

sMonths(8) = "August"

sMonths(9) = "September"

sMonths(10) = "October"

sMonths(11) = "November"

sMonths(12) = "December"



Dim sMonthList

sMonthList = Join(sMonths, ",")

sMonthList will contain "January,February,March,April,May,June,July, August,September,October,November,December". Notice that there are no spaces inserted between the month names; only the specified delimiter-in this example, a comma-is inserted between the list elements.

Working with Array Data

You can use numeric variables to represent array elements when accessing arrays. For example, the following example works fine.


Dim sMonths(12)

sMonths(1) = "January"

sMonths(2) = "February"

sMonths(3) = "March"

sMonths(4) = "April"

sMonths(5) = "May"

sMonths(6) = "June"

sMonths(7) = "July"

sMonths(8) = "August"

sMonths(9) = "September"

sMonths(10) = "October"

sMonths(11) = "November"

sMonths(12) = "December"



iMonth = InputBox("Enter a number from 1-12")

MsgBox "You selected " & sMonths(iMonth)

The last line of this example uses the variable iMonth to dynamically access a given element in the array sMonths.

NOTE

You'll see a number of examples of arrays in administrative scripts later in this book. For now, just know what an array looks like, and remember that it's a collection of values assigned to a single variable name. It will all come together for you later on; so if you don't see a clear use for arrays yet, don't worry. You will!


One last trick is the IsArray() function. This function accepts a variable, and returns True or False depending on whether the variable is an array.

    Previous Section Table of Contents Next Section