Professional Documents
Culture Documents
com
Visit:
www.gcreddy.com
www.gcreddy.com 1
gcreddy@gcreddy.com
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:
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"
VBScript has its own built in method for creating arrays in bulk rather
than having to do it element by element:
www.gcreddy.com 2
gcreddy@gcreddy.com
dim name
name = array("Fred", "Jane", "Henry")
msgbox name(1) 'Jane
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
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:
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
dim person(1,2)
person(0,0) = "Fred"
person(0,1) = "Jane"
person(0,2) = "Henry"
www.gcreddy.com 3
gcreddy@gcreddy.com
person(1,0) = 21
person(1,1) = 21
person(1,2) = 45
msgbox person(0,1) & " is " & person(1,1) 'Jane is 21
Dynamic Arrays
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 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)
www.gcreddy.com 4
gcreddy@gcreddy.com
person(0,1) = "Jane"
person(1,1) = 21
msgbox (ubound (person, 2)) '1
Summary
• 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.
www.gcreddy.com 5