Introduction to VBScript Arrays: VBScript Tutorial #7
In my previous tutorial in VBScript tutorial series, we discussed ‘Procedures and Functions’ in the VBScript. In this tutorial, I will be discussing the concept of ‘VBS Arrays’. One should have a good understanding of the concept of Arrays for better programming experiences.
At times there may be a need to store more than 1 element in a single named memory location and the concept of Arrays comes into the picture to satisfy this requirement.
As we have already learned about Variables in one of the earlier tutorials, it will be easy to understand this concept as an Array is also a variable but with the difference that it can contain more than 1 value at a time.
This tutorial gives you a wide knowledge about Arrays, their types, their declaration in VBScript, etc., with simple practical examples for your easy understanding.
Table of Contents:
What is an Array?
An Array is a variable having a named memory location that serves as a Container and can hold multiple values in a single location.
In short, Arrays group different types of elements together in one place.
Let’s take a real-life example to understand this better. If you want to store the names of different students in a single place then an Array of string types can be used starting at index 0. If you want to fetch the name of the first student then you can pick the value present at index 0 and so on.
Suggested reading =>> Arrays in VBA
Now, let’s move ahead to the next topics to learn how Arrays are actually declared and used in the script.
Declaration of Arrays in VBScript
Declaration of an Array can be done in the same manner in which Variables are declared but with the difference that the array variable is declared by using parenthesis ‘()’.
The Dim keyword is used to declare an Array.
Ways to declare an Array:
There are 3 ways in which an Array can be declared.
They are as follows:
#1) Way 1: Dim array1()
Here, array1 is the name of an array and as the parenthesis is empty it means that the size of an array is not defined here.
If you want to declare an array by mentioning its size then it can be done in the following way.
#2) Way 2: Dim array1(5)
In this, array1 is declared with the size as 5 which states it holds 6 values considering that the index of an array always starts from 0. These 5 values can be of integer type, string, or character types.
#3) Way 3 : array1 = Array(1,2,3,4,5,6)
Here, Array Function is used to declare an array with a list of arguments inside the parenthesis and all integer values are passed directly inside the parenthesis without any need of mentioning the size of an array.
Note: The index value of an Array can never be a negative value.
Next, let’s discuss how to assign values to an array.
Assignment of Values Inside an Array
Once an Array is declared, values are assigned to an Array Variable. To assign values, each specific index location is accessed as values are assigned specifically to the index values in an Array.
Taking reference to the second way of declaring an array (as discussed above), let’s see how to assign values to such an array.
Dim array1(5)
Here, the size of an array is 6, which means you have to assign 6 values to an array starting at index 0 and ending at 5.
So, the following is the way to do so:
array1(0) = “hello”
array1(1) = 12
array1(2) = 13
array1(3) = 14
array1(4) = 15
array1(5) = 16
Each index has one specific value.
Following is an example to show the usage of an Array:
<html> <head> <title>Let’s see implementation of an Array</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim array1(5) array1(0) = “hello” array1(1) = 12 array1(2) = 13 array1(3) = “how are you” array1(4) = 15 array1(5) = 16 For i = 0 to ubound(array1) Msgbox “Value present at index ” & i & ” is “ & array1(i) & “<br />” Next </script> </body> </html>
Output is:
Value present at index 0 is hello
Value present at index 1 is 12
Value present at index 2 is 13
Value present at index 3 is how are you
Value present at index 4 is 15
Value present at index 5 is 16
In the above example, an array of size ‘5’ is declared and the values are assigned to each index which is the combination of integer and string values. Next, using ‘For loop’, the value present at each index is displayed with the help of a message box. Loop will start from 0 and will go till the unbound i.e. upper bound which is the maximum subscript(5 in this case) of an array.
I will discuss unbound later in this tutorial.
Types of Arrays
There are basically 2 types of Arrays that are used in the VBScript.
They are:
#1) Single Dimensional Array:
This is a simple type of array which is used more often in the scripts, than the one which is discussed above
#2) Multi-Dimensional Array:
When an array has more than 1 dimension then it is known as a multi-dimensional array. Normally, a 2-Dimensional Array is the one which is used most of the times i.e. there will be rows and columns in an array. The maximum dimension of an array can reach up to 60.
Let’s understand the working of a 2 Dimensional Array with the help of a simple example.
Example:
<html> <head> <title>Let’s see implementation of a 2 Dimensional Array</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim array1(1,1) array1(0,0) = “hello” array1(0,1) = 12 array1(1,0) = “how are you” array1(1,1) = 14 Msgbox “Value present at index 0,0” & ” is “ & array1(0,0) & “<br />” Msgbox “Value present at index 0,1” & ” is “ & array1(0,1) & “<br />” Msgbox “Value present at index 1,0” & ” is “ & array1(1,0) & “<br />” Msgbox “Value present at index 1,1” & ” is “ & array1(1,1) </script> </body> </html>
Output is:
Value present at index 0,0 is hello
Value present at index 0,1 is 12
Value present at index 1,0 is how are you
Value present at index 1,1 is 14
In the above example, an array having 2 rows and 2 columns is declared with the size as (1,1) representing values present at the indexes 0 and 1 for both row and column.
Next, let’s understand some of the frequently used concepts of an Array.
Usage of REDIM Statement and PRESERVE Keyword in an Array
Redim Statement is used to redefine the size of an Array. When the array is declared without any size, then it can be declared again using Redim with the feasibility of specifying the size of an array.
The preserve keyword is used to preserve the contents of a current array when the size of an array gets changed.
Let’s understand the usage of these keywords with the help of a simple example.
Example:
<html> <head> <title>Let’s see implementation of Redim and Preserve</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim array1() REDIM array1(3) array1(0) = “hello” array1(1) = 12 array1(2) = 13 array1(3) = “how are you” REDIM PRESERVE array1(5) array1(4) = 15 array1(5) = 16 For i = 0 to ubound(array1) Msgbox “Value present at index ” & i & ” is “ & array1(i) & “<br />” Next </script> </body> </html>
Output is:
Value present at index 0 is hello
Value present at index 1 is 12
Value present at index 2 is 13
Value present at index 3 is how are you
Value present at index 4 is 15
Value present at index 5 is 16
In-Built Array Functions
There are various inbuilt functions related to Arras that is supported by the VBScript.
Following is the list:
#1) lbound:
This is the opposite of ubound (used above). This returns the smallest integer index value of an array i.e. the smallest subscript of an array.
Example: In the above example, the size of an array is 5. Hence, lbound will be 0 as this is the smallest subscript of an array.
#2) ubound:
This is already used above. This returns the largest subscript of a defined array.
Example: In the above example, the size of an array is 5. Hence, in this case, unbound is 5
#3) Split:
This returns an array consisting of a number of sub-strings and can be split using some delimiter. The syntax of this is: Split(expression,[delimiter])
Using a delimiter is an optional condition.
#4) Join:
This is the opposite of the Split function. Here, String is returned which includes various substrings in an array and thus joins all the sub-strings into one string.
The syntax of this is: Join(array,[delimiter]. Using a delimiter is an optional condition.
#5) IsArray:
This returns True/False on the basis of a specified variable. If the variable is passed is an Array then True is returned else False.
The syntax is: IsArray( array variable)
#6) Filter:
This returns a subset of an array based on the filter condition i.e. data is filtered on the basis of some condition.
The syntax is: Filter(array, filter condition)
Let’s see the implementation of these functions with the help of a simple Example.
Example:
<html> <head> <title>Let’s see implementation of In-Built Array Functions</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim array1 = Array(“January”, ”February”, ”March”, ”April”) Dim a , b , c , d , e , f a = lbound(array1) b = ubound(array1) c = Split(array1,”,”) d = Join(array1,” $ “) e = IsArray(array1) f = Filter(array1,”J”) Msgbox(a) & “<br />” Msgbox(b) & “<br />” Msgbox(c) & “<br />” Msgbox(d) & “<br />” Msgbox(e) & “<br />” Msgbox(f) </script> </body> </html>
Output is:
0
3
January February March April
January $ February $ March $ April
True
January
Conclusion
I hope that this tutorial would have given you a brief overview of Arrays in VBScript. The simple practical examples covered must have made you understand arrays in a better way.
Next VBScript Tutorial #8: Our next tutorial will cover ‘Date Functions’ in the VBScript.
We would be glad to hear about your experience with working on Arrays in VBScript and let us know if you have any queries.