VBScript Array Functions – An Excellent Guide for VBScript Tutorial 8

vbscript array functions

VBScript Tutorial – Table of Content

VBScript Tutorial #1: Overview of VBScript Variables 

VBScript Tutorial #2: VBScript Conditional Statements and Loops

VBScript Tutorial #3: VBScript Procedures

VBScript Tutorial #4: VBScript Error Handling and Execute VBScript

VBScript Tutorial #5: VBScript String Functions

VBScript Tutorial #6: VBScript Date Functions

VBScript Tutorial #7: VBScript Time Functions

VBScript Tutorial #8: VBScript Array Functions

VBScript Tutorial #9: VBScript Math Functions

VBScript Tutorial #10: VBScript Conversion Functions and VBScript Format Functions

VBScript Tutorial #11: VBScript Other Functions 

Through out this “VBScript Array Functions” article, we will explains the different types of frequently used vbscript array functions with examples. The important functions related to array are vbscript join, vbscript array, vbscript filter, vbscript split, etc.

VBScript Tutorial #8: VBScript Array Functions

VBScript Array Functions:

While working with arrays in vbscript, we can use in-build vbscript array functions to perform important array-related operations such as create, manipulate, conversion, etc. This article (VBScript Array Functions) contains all the important built-in VBScript array functions, which are mostly used in programs. 

VBScript Array Functions – Summary:

  • vbscript Array – Defines an array based on given data values.
  • vbscript Filter – Defines a array which is a subset of another one-dimensional string array. The new array is created based on filter criteria.
  • vbscript IsArray – Test a array variable and return a Boolean value based on the result.
  • vbscript Join – Converts an array and returns a string value where all the array elements are separated by a specific delimiter.
  • vbscript Split – Converts a string into a zero-based, one-dimensional array.
  • vbscript LBound – Returns the lower index of an array.
  • vbscript UBound – Returns the upper index of an array that indicates the dimension.

VBScript Array Functions – Details:

All the important vbscript array functions are explained in this section with a proper example.

vbscript Array:

vbscript array function defines an array based on given data values. The array elements are passed the arguments in the vbscript array function.

Syntax: Array(arglist)

Parameter Description:

arglist – These are the mandatory parameters. The list(separated by comma) of arguments are basically the elements of the array.

Example:

In the below example, we will create an array of weekdays using vbscript array function and display the first day of the week (0 index) in a message box.

dayArray = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
msgbox "The first day of week: " & dayArray(0)

Output (Message Box): 
The first day of week: Mon
vbscript array functions - vbscript array
vbscript array functions – vbscript array

vbscript Filter:

vbscript filter function defines a zero-based array that contains a subset of a one-dimensional string array. The one-dimensional new array is created based on filter criteria.

Syntax: Filter(string_array,value[,include[,compare]])

Parameter Description:

string_array – It’s a mandatory parameter which indicates a one-dimensional array of string.

value – It’s a mandatory parameter which represents the filter criteria, i.e. the string expression to search in the array.

include – It’s an optional Boolean parameter. If we provide “true” value as include parameter, it includes the elements which contain the searched criteria. Else, it will exclude the elements which contain the criteria. The default value is true.

compare – This is also an optional parameter which specifies the comparison type as binary or textual. If not specified, by default parameter value will be treated as zero. The possible values are – 

· 0 = vbBinaryCompare – Perform a binary checking

· 1 = vbTextCompare – Perform a textual checking

Example:

In the below example, we will create an array using vbscript filter function based on the elements of the weekday array, which contains “S” character.

dayArray = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
filterArray = Filter(dayArray, "S")
for each e in filterArray
	msgbox e
next

Output (Message Box): 
Sat
Sun

vbscript IsArray:

vbscript isarray function tests and returns a boolean value after checking a specified variable is an array or not. For a valid array, the return value is true else false will be returned.

Syntax: IsArray(variable)

Parameter Description:

variable – It’s a required parameter which needs to be verified.

Example:

In the below example, we will check a variable if it’s an array or not.

dayArray = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
boolFlag = IsArray(dayArray)
msgbox "Return value: " & boolFlag

Output (Message Box): 
Return value: True

vbscript Join:

vbscript join function converts an array into a string expression where all the array elements are separated by a specific delimiter.

Syntax: Join(array [, delimiter])

Parameter Description:

array – It’s a required parameter which represents a one-dimensional array.

delimiter – It’s an optional parameter which is used to separate each array element after converting into string expression.

Example:

In the below example, we will convert the weekday array into a string expression using vbscript join function where all the elements will be separated by a comma.

dayArray = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
dayString = Join(dayArray, ",")
msgbox "Converted week day string: " & dayString

Output (Message Box): 
Converted week day string: Mon,Tue,Wed,Thu,Fri,Sat,Sun
vbscript array functions - vbscript join
vbscript array functions – vbscript join

vbscript Split:

vbscript split function converts a string into a one-dimensional array where array elements are created based on specific delimiters.

Syntax: Split(expression[, delimiter[,count[,compare]]])

Parameter Description:

Expression – It’s a required parameter which represents a string expression.

delimiter – It’s an optional parameter which is used to differentiate each array elements within the string expression. The default value is space.

count – It’s an optional parameter which represents the count of substring/array elements to be returned. The default value -1 specifies that entire string will be returned as single element of the array.

compare – This is also an optional parameter which specifies the comparison type as binary or textual. If not specified, by default parameter value will be treated as zero. The possible values are – 

· 0 = vbBinaryCompare – Perform a binary checking

· 1 = vbTextCompare – Perform a textual checking

Example:

In the below example, we will convert a string expression, contains all the day name of a week which are separated by semi-column, using vbscript split function. After the conversion, we will display the first and last day of a week.

string_expression = "Mon;Tue;Wed;Thu;Fri;Sat;Sun"
dayArr = Split(string_expression, ";")
msgbox "First day-> " & dayArr(0) & " and Last day-> " & dayArr(6)

Output (Message Box): 
First day-> Mon and Lat day-> Sun
vbscript array functions - vbscript split
vbscript array functions – vbscript split

vbscript LBound:

vbscript lbound function return the lower index, i.e. smallest subscript of an array for the specified dimension. The lbound value for an array is always 0.

Syntax: Lbound(array[,dimension])

Parameter Description:

array – It’s a required parameter which represents a one-dimensional array.

dimension – It’s an optional parameter which indicates the dimension of the array for which smallest subscript will be returned. The value will be 1 for the first dimension, 2 for the second dimension and so on. The default value is 1. 

Example:

In the below example, we will find and display the lower subscript value using vbscript lbound function.

string_expression = "Mon;Tue;Wed;Thu;Fri;Sat;Sun"
dayArr = Split(string_expression, ";")
msgbox "Lbound Value-> " & Lbound(dayArr)

Output (Message Box): 
Lbound Value-> 0

vbscript UBound:

vbscript ubound function return the upper index, i.e. the largest subscript of an array for the specified dimension. The ubound value for an array represent the highest array index i.e. number of element minus one. This function helps to calculate the length of an array.

Syntax: Ubound(array[,dimension])

Parameter Description:

array – It’s a required parameter which represents a one-dimensional array.

dimension – It’s an optional parameter which indicates the dimension of the array for which smallest subscript will be returned. The value will be 1 for the first dimension, 2 for the second dimension and so on. The default value is 1. 

Example:

In the below example, we will find and display the longest subscript value using vbscript ubound function.

string_expression = "Mon;Tue;Wed;Thu;Fri;Sat;Sun"
dayArr = Split(string_expression, ";")
msgbox "Ubound Value-> " & Ubound(dayArr)

Output (Message Box): 
Ubound Value-> 6
vbscript array functions - vbscript ubound
vbscript array functions – vbscript ubound

Conclusion:

Through this VBScript Array Functions article, we have learned about the frequently used VBScript Array Functions such as, vbscript array, vbscript filter, vbscript join, vbscript split function, etc. In the next vbscript tutorial, we will explain more functions on VBScript functions. Please click to read more on vbscript from here.

About K Mondal

Hi, I am K. Mondal, I am associated with a leading organization. I am having 12+ years of working experience across domains e.g., application development, automation testing, IT Consultant. I am very much interested in learning different technologies. I am here to fulfill my aspiration and currently contributing as an Author and Website Developer both in LambdaGeeks.
Connect to LinkedIn- https://www.linkedin.com/in/kumaresh-mondal/

Leave a Comment

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