Windows Scripting: VBScript and Arrays

How to Create Multidimensional and Dynamic Arrays with VBScript

© Mark Alexander Bain

Dec 7, 2008
Creating Arrays with VBScript, Mark Alexander Bain
Arrays are easy to create in any programming language and can be static or dynamic, one dimension wide or multidimensional; and it's even easier with VBScript.

Arrays are a very useful and easy way of storing variables - and they're especially easy to use in VBScript. This is due to several factors:

  • VBScript is particularly liberal with any variable definition - that means that there is no strict defining of variables to a particular data type
  • the data type is assigned automatically when the variable is loaded with a value
  • it's even possible to mix data types within the same array

It is also possible to define the arrays in different ways, for example:

  • create the array element by element
  • use the VBScript array method
  • use the VBScript splitmethod

It's even possible to create multidimensional arrays and to make them dynamic rather than static.

Creating a Simple VBScript Array

The simplest VBScript array is created by using the dim statement (as with other variables) however the array also needs to have it's highest index number defined (the lowest index being 0):

dim name(2)

In this case an array with three elements (0, 1 and 2) has been defined; and then it's just a matter of assigning values to the elements:

name(0)="Fred"
name(1)="Jane"
name(2)="Henry"

And then the contents of the array can be used as required:

msgbox name(0) 'Fred

The VBScript Array Method of Creating Arrays

VBScript has its own built in method for creating arrays in bulk rather than having to do it element by element:

dim name
name = array("Fred", "Jane", "Henry")
msgbox name(1) 'Jane

The VBScript Split Method of Creating Arrays

VBScript can also create arrays from information such as csv (comma separated variable) data:

dim name
dim details
details = "Fred,Jane,Henry"
name = split (details, ",")
msgbox name(2) 'Henry

Calculating the Size of Arrays with Ubound

If an array is created using the array method or the split method then the actual size of the array may not be known; and that's where the ubound method is useful - this returns the highest index number of the array:

msgbox ubound(name) '2

The size of the array is, of course, one more than the highest index number (since the array starts at index number 0).

Multidimensional Arrays

As the amount of information increases (for example storing an age as well as a name) then the programmer has two choices:

  • create multiple arrays - one for each piece of information
  • use a multidimensional array where each dimension represents a different aspect of the data

The multidimensional array is created in a similar way to the standard array; for example the following creates a two by three dimension array (two pieces of information for three people):

dim person(1,2)
person(0,0) = "Fred"
person(0,1) = "Jane"
person(0,2) = "Henry"
person(1,0) = 21
person(1,1) = 21
person(1,2) = 45
msgbox person(0,1) & " is " & person(1,1) 'Jane is 21

Multidimensional Arrays and Ubound

If the size of a multidimensional array needs to be calculated then the ubound method can still be used, but this time the dimension number needs to be included:

msgbox ubound(person, 1) 'The highest index of the first dimension
msgbox ubound(person, 2) 'The highest index of the second dimension

Dynamic Arrays

There is a major disadvantage to arrays - they're static and have a fixed size; in the above examples three, and only three, elements can be worked with; if a fourth person (for example) were to be added then an error would occur. The solution is to use the redim statement rather than the dim statement:

redim name(0)
name(0) = "Fred"
msgbox (ubound (name)) '0

Now the redim statement can be used to resize the element (the preserve key word ensures that any existing data is not lost):

redim preserve name(ubound(name) + 1)
name(1) = "Jane"
msgbox (ubound (name)) '1
msgbox name(ubound (name)) 'Jane

Dynamic Multidimensional Arrays

Multidimensional arrays can be dynamic but only one of the dimensions (the 'right hand' one) can be changed:

redim person(1,0)
person(0,0) = "Fred"
person(1,0) = 21
msgbox (ubound (person, 2)) '0
redim preserve person(ubound (person, 1), ubound (person, 2) + 1)
person(0,1) = "Jane"
person(1,1) = 21
msgbox (ubound (person, 2)) '1

Summary

Arrays are an easy way to group and store variables in VBScript. They're created by:

  • using dim and the maximum index number of the array
  • using the VBScript array method
  • using the split method

Arrays can be:

  • multidimensional
  • dynamic (although only the 'right hand' dimension of a multidimensional array can be changed)

Finally the size of any array can be found by using the ubound method.


The copyright of the article Windows Scripting: VBScript and Arrays in Windows Programming is owned by Mark Alexander Bain. Permission to republish Windows Scripting: VBScript and Arrays in print or online must be granted by the author in writing.


Creating Arrays with VBScript, Mark Alexander Bain
A Multidimensional VBScript Array, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Jun 9, 2009 2:58 AM
Guest :
good one
Aug 12, 2009 12:28 AM
Guest :
Good...working
Sep 17, 2009 12:21 AM
Guest :
Dear Mark, How can I obtain the number of dimensions of an array?
Sep 17, 2009 7:18 AM
Mark Alexander Bain :
Have a read of my blog: http://www.suite101.com/blog/linuxtalk/vbscript_and_multiple_arrays
4 Comments