Array is a data type used to describe a collection of elements. An array can be one-dimensional or multidimensional array. In VBScript, array can be represented as follows:
Dim arrayOned(6) - represent a dimension array which can store 7 elements. Index starts
from 0.
Dim arraytwod(6)(4) - represents a two dimensional array
which can store 7*5 =35 element
Dim(6)(4)(5) - Represents a three dimensional array. We can describe
n-dimension array in similar ways.
The above arrays are example of fixed array. Once we specify
an array with dim and providing size of array, it is created as fixed dimension
array. We cannot change the size of the array.
We can also create dynamic array in VBScript as shown below:
dim arraydynam() - create a variable without providing the size of array.Redim arraydynam(10) - we can then assign a size to the array by using redim. We can resize the array size as when required.
On reducing
the size of the array, loss of information can take place, so care should be
taken while reducing the size of array in dynamic array.
There are some useful inbuilt functions in VBScript used to work with arrays. Let us discuss on the same.
Split Function - Split can be used to split a string into array based on the delimiter provided. Syntax for Split function is:
Split (expression [, delimiter [, count [, compare]]]) -
here expression denotes the string which will be splitted based on the
delimiter provided.
Count and compare are optional with default value as -1
(repeats all substring) and binary compare. Default delimiter used is space.
Ubound function - Ubound returns largest subscript for the provided dimension of an array. In Case of argument for dimension not provided, it gives the ubound for first dimension
UBound(array, dimension))
Lbound function - Lbound returns smallest subscript for the provided dimension of an array. In Case of argument for dimension not provided, it gives the lbound for first dimension
UBound(array, dimension))
Join Function - Join function does opposite of what Split function does. It joins all the elements in the array using the delimiter. Syntax of function is:Join(array, delimiter)
In case of join also, the default delimiter if not provided is
space character.
Filter Function - Filter is used to create a sub array from
array matching the particular condition.
Filter(array, value, include, compare) - In the function,
include and compare are optional parameters, include can have value true or false,
true for searching for element with particular value matching and false to
return elements which do not have match the condition
Array function - This function creates an array based on array element provided in the argument.
e.g. : a = array ("testing ", "array",
"by" ,"creating", "array", "using array
functions)
IsArray function returns true/false that indicates whether a specified variable is an array or not
''A simple code explaining the various functions for array manipulation
dim arr, strjoin
arr = split("this,is, wonderfrul, test",",") ' create an array using split function
ilowbound = lbound(arr) 'get lowerbound and upperbound for array
iupperbnd = ubound(arr)
for i=ilowboud to iupperbnd Step 1 '' Loop the array element
msgbox arr(i)
Next
b= filter(arr, "t")
msgbox b(1)
strjoin = join(arr, ";")
msgbox strjoin
msgbox Isarray(arr)