VBScript Arrays

In VBScript, an array is a special type of variable that can contain multiple values of varying types. This is different from other languages, such as C# and Java, where the values are restricted to being all of the same type.

The following variable, ‘fname’, has been defined and initialised with the name ‘Bob’.

Dim fname
fname = "Bob"

If multiple names need to be stored, instead of having a different variable for each, an array could be used.

Dim fname(3)
fname(0) = "Bob"
fname(1) = "George"
fname(2) = "Fred"
fname(3) = "Alan"

The first line in the above example declares an array, which holds four elements. Each element in an array has an index number, which is used in lines two to five above to populate the four elements. Notice that the first index number is a zero and not a one. The number three in the array declaration refers to the maximum index value allowed in the array and not the actual number of elements allowed in the array, hence why it can store four elements and not just three.

The above example can be shortened by adding all the values in one statement. Here the maximum index value is not specified when the array is declared.

Dim fname
fname = array("Bob", "George", "Fred", "Alan")

As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.

WScript.Echo fname(2)

Here, the element with an index number of two would be displayed, in this case “Fred”.

In order to display all of the elements in an array, a ‘For’ loop can be used.

For i = LBound(fname) To UBound(fname)
    WSCript.Echo fname(i)
Next

The above example uses the ‘LBound’ function for the starting value of ‘i’, which equates to the lowest index number in the array, zero, and the ‘UBound’ function to get the highest index number in the array, therefore specifying the number of iterations through the loop. The resulting output will list all the elements of the array on a separate line.

Bob
George
Fred
Alan

Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, instead of just storing first names in the array, the last names could also be stored.

Dim names(3, 1)
names(0, 0) = "Bob"
names(0, 1) = "Smith"
names(1, 0) = "George"
names(1, 1) = "Jones"
names(2, 0) = "Fred"
names(2, 1) = "Bloggs"
names(3, 0) = "Alan"
names(3, 1) = "White"

In order to display the names in the console, nested ‘For’ loops need to be used, with the outer loop cycling through the sets of values, in this case, the first name and last name pairs, whilst the inner loop cycles through the values within each set. Within the inner loop it determines whether it is the first name or the last name by using the index number, with the first name having an index number of zero. If it’s the first name, then it gets added, along with a space to a variable. If it is the last name then this gets added on to the end of the variable, which is then displayed.

Dim fullName

For i = LBound(names, 1) To UBound(names, 1)

    For j = LBound(names, 2) To UBound(names, 2)

        If j = 0 Then
	
            fullName = names(i, j) & " "
		
        Else
		
            WScript.Echo fullName & names(i, j)
	
        End If
	
    Next

Next

The output from this is shown below.

Bob Smith
George Jones
Fred Bloggs
Alan White

Nested Arrays

A variation on multidimensional arrays, uses nested arrays, which basically comprises of an array of arrays. The outer ‘for’ loop uses the ‘LBound’ and ‘UBound’ values of the outer array, whilst the inner ‘for’ loop uses the ‘LBound’ and ‘UBound’ values of the first inner array. The resulting output is the same as above.

Dim names

names = array( _ 
              array("Bob", "Smith"), _ 
	      array("George", "Jones"), _ 
	      array("Fred", "Bloggs"), _ 
	      array("Alan", "White") _ 
             )

Dim fullName

For i = LBound(names) To UBound(names)

    For j = LBound(names(0)) To UBound(names(0))

        If j = 0 Then
	
            fullName = names(i)(j) & " "
		
        Else
		
            WScript.Echo fullName & names(i)(j)
	
        End If
	
    Next
	
Next