How to Work With Arrays in VBScript or UFT?

Arrays in VbScript or UFT
Arrays in VbScript or UFT
Arrays in VbScript or UFT image credit-wiki

Arrays in Vbscript or arrays in UFT

A variable containing a series of values, is called an array variable.Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.

Example:

Ads code goes here
Dim ArrayName(Dimension)
'like-
Dim A(3) 

Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements.

In order of save this information we need to create 4 scalar variables. And if the data structure size goes high we need to create a series of scalar variables.

This way of creating multiple scalar variables may lead to unwanted failure of the script. Hence the better approach is to create a Array (as a collection).

What is an array and how to assign values to Array in VBScript?

An array is a list of related data governed by index or subscript. An array can be viewed as a column and row combination.Arrays have dimensions.Vbscript supports upto 60 dimensions.
1-dimensional array
Sl NoStud_NoStud_NameStud_Email
1Stud1XYZ[email protected]

How to Assign values

We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows:

' For 1 dimensional array
A(0) = 256
A(1) = 324
A(2) = 100
A(3) = 55
' For 2 dimensional array
A(1,1)="ABC"
A(1,2)="[email protected]"

Similarly, the data can be retrieved from any element using an index into the particular array element you want.

For example:

SomeVariable = A(4) 

Static Array

Static Array is an array whose size is defined at the beginning of the script.

Multidimensional Array

An array can store 60 dimensions.

2-dimensional array

The syntax is the same:

Dim arrayName(Dimensions)

The below one is studentInfo(2,3). It is a representation of ArrayName(X, Y).X and Y corresponds to an intersection of the points of each dimension. In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns

As the array is zero based hence the total number of information that is saved here is (2+1)*(3+1)=12

Sl NoStud_NoStud_NameStud_Email
1Stud1XYZ[email protected]
2Stud2ABC[email protected]

Each cell corresponds to an element of the array can hold a single value.

Arrays aren’t limited to a single dimension. We can have as many as 60 dimensions, although most people can’t comprehend more than three or four dimensions.
In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

Dim MyTable(5, 10)

How to Use Ubound() function for multidimensional array?

The use of Ubound() function for the multidimensional array is little tricky:

Lbound(nameOfArray,dimension)

nameOfArray is the array whose Ubound needs to be calculated on the given dimension.

READ  Learn UFT Step By Step Manner in a Fast Pace

Dynamic Arrays:

Resizing the single-dimensional array

Till this time all the discussion happened on array is of the fixed size where the VBScript engines know the size of the array. We can also declare an array whose size changes during the time our script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the ReDim statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses.

For example:

Dim MyArray()
ReDim AnotherArray() 

To use a dynamic array, we must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30 but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)
ReDim Preserve MyArray(30) 

There is no limit to the number of times we can resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.

We can declare a variable without the array notation like MyArray and later can be redim to a multidimensional array.

Resizing the multidimensional array

Multidimensional array works almost in the same way as single-dimensional array except for few exceptions:

  • If myArray(4,4) is a multidimensional array, we can only make a change to the last dimension, so myArray(4,10) is legal. Using preserve a redim keyword we can save the data.
  • If myArray(4,4) is a multidimensional array, we can change all the dimensions. So myArray(5,10) is also legal. But in this case, we can not use preserve keyword and the existing data will be lost

Where to use a dynamic array?

When we do not know the size or the volume of the incoming data to our script, we need to use a dynamic array.

Example:

  1. Customer survey
  2. Customer details  etc

In these scenarios, we beforehand don’t know how many customer we are going survey and how many of the customer will give me the details. So in those scenarios, if we use a fixed-size array, we may eat up space if we get more than what we anticipated or we may lose memory space in case we do not get the anticipated numbers.

In such uncertain conditions, we need to use a dynamic array.

VarSafety of Variant in Array:

We can use dynamic array using a double bracket “()”.Hence the case-I is a normal variable. We can change the normal variable to array by using redim keyword. The transformation is done by the VBScript engine. But this is not a good coding style to use a multipurpose variable and change the type. We can test the types via debugging.

//case-I
Dim a
a=5
redim a(2,0)
a(0,0)=5
//case-II
Dim b
redim b(2,0)
b(0,0)=5 

Looping through Array:

we can loop through an Array using Index. The important factors are:

  • Index
  • The upper bound of an Array function name-UBound
READ  How to Use VBScript Variables?

Ubound function takes two arguments:

  1. Name of the array variable
  2. The number of the dimension

Ubound and Lbound function of Array

When UBound() Function provides the upper limit or the maximum size of the Array, the Lbound() function provides the lowest non-negative size of the array.

Ubound() provides the maximum no of an index , a coder can access. Hence it eliminates the possibility of accessing an index outside of the Array.

If we attempt to access an index outside of the array, VBScript engine will throw an error stating “Subs script out of range”.

Syntax of Ubound() and Lbound()

msgbox Ubound(arrayName)
msgbox Lbound(arrayName)

Let’s see the example:

Dim a()
redim preserve a(1,1)
a(0,0)=1
a(0,1)=3
a(1,0)=5
a(1,1)=7
for intIndex=0 to UBound(a,1)
msgbox a(1,intIndex)
next 

Other methods to access an array

Direct Access of a VBScript Array

We can perform the direct access method to access an element from an Array.

Like

msgbox arrStudInfo(2,1)

For Each..Loop

For Each element in Array
msgbox element
Next

How to erase an Array in VBScript or UFT or QTP:

Erase command in VBScript will erase the content the array. Once we apply Erase command to a fixed-sized array the data elements are emptied but the size of the array that is the memory block of the Array is kept as it is.

Erase arrayName 

While we apply to Erase on the dynamic array, the entire memory is released along with the dimensions, in that case, it becomes normal one-dimensional array or variant.

We need to redim again to insert multiple dimensions to it.

Two functional that make Array powerful

VBScript provides two functions that are very useful while working with an array. They are as follows:

  • Array()
  • IsArray()

Array() Function

This function provides an ability to the coder to quickly define an array. So it can retrieve a variant that is containing the array.

Syntax:

Array()
Array(argumentList)

argumentList is the list of arguments that needs to be stored in the array as an individual element. If omitted like the first case, it will create a zero-length array.

Example:

arrMyArray=Array("ABC","XYZ",1,"[email protected]")

IsArray() Function

IsArray() function checks if a variable or variant is of type array or not. It returns True if the variable under test is an array, False otherwise.

Syntax:

IsArray(nameOfArray)

Example


dim flag
Redim myArray(10)
flag=IsArray(myArray)
If(flag) then
msgbox "The variable is of type Array"
else
msgbox "The variable is not of type Array"
End If

Advantages of Array

  • We do not need to create a lot of Scalar variables. Instead, a single array is enough to hold the values of the same type of a collection.
  • Being a single variable, it is easy to manage.
  • By specifying the name of the array,whole collection can be referenced and used.

Also check VBScript data type here for more clarity.

In the same context we need to learn two more important concepts:

  • Passing variables by reference i.e pass by ref
  • Passing variables by value i.e pass by value

Passing variable value using byRef and byVal

We can pass a variable value to a function or procedure for by using two techniques.

Pass By Value

This is by default operation. Hence Byval keyword is optional.the caller function transfer the variable value with the function. The procedure/ function has equivalent variables to take these values. Any change to the variable temporary inside the function.

Pass By Reference

The arrangement is indicated by By Ref keyword.The caller function shares the variable to the procedure or function. Any change to the variable is permanent.

READ  Regular Expressions in VBScript or QTP or UFT

Exp:-

Option Explicit 
Dim myvarfirst 
Dim myvarsecond 
myvarfirst=1
myvarsecond=1
Checkvar myvarfirst,myvarsecond 
msgbox myvarfirst & myvarsecond 

Sub checkvar( byRef myvarfirst,Byval myvarsecond)
//Byval is optional declaration 
myvarfirst=myvarfirst+1 
myvarsecond=myvarsecond+1 
End sub
Scalar Variables and Array Variables

Much of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 11 elements is declared:

Dim A(10)

Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.

You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows:

A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(10) = 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:

. . .
SomeVariable = A(8)
. . .

Arrays aren’t limited to a single dimension. You can have as many as 60 dimensions, although most people can’t comprehend more than three or four dimensions. You can declare multiple dimensions by separating an array’s size numbers in the parentheses with commas. In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns:

dim MyTable(5, 10)

In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.

You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the ReDim statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example:

Dim MyArray()
ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place.

ReDim MyArray(25)
. . .
ReDim Preserve MyArray(30)

There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.

Use VarType() with Arrays

Data contained in the variable can be of any type, but variables itself are always of Variant type. Checking with VarType the contents of a position of an array will return the type of data contained. But the array itself is a compound of “cells” of Variant type.

so

varType=VarType(MyArray)-VbArray
varType=VarType(MyArray)-8192

Reference- Stack Overflow.

Share and Enjoy !

1 thought on “How to Work With Arrays in VBScript or UFT?”

Leave a Comment

Your email address will not be published. Required fields are marked *