Arrays
Arrays: An array is a variable that holds multiple values of the same type.
Arrays are of two types: One is fixed and another is dynamic.
Fixed arrays can be simply defined by using dim statement:
Dim arr(10)
this array can contain eleven values as array always start from 0
' Example:
Dim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
Another simple method can be as follows:
Dim arr
arr=Array(1,2,3,4,5,6,7,8,9,10)
This will define the array of size of 9 automatically having values from 1 to 10.
To Define dynamic arrays we will not specify the dimensions or length of the array when declaring the array
we will use the redim statement for specifying the dimensions like
Dim arr() ' this will initialize a array since we have not given dimensions in it we can not use it
ReDim arr (10) 'this will initialize the array to a dimension of 10 i.e. 11 elements.
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10 he
arr(i)= "Value" & i
Next
This will give you the same result
One drawback of this approach is that when you use the redim the values stored in the arrays will be lost:
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
ReDim arr(3)
when u will look into the array after the above code. The array will look like something shown below:

To overcome this we can use the Preserve keyword
Preserve: Preserves the data in an existing array when you change the size of the last dimension.
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
ReDim preserve arr(3)
Following are the list of functions that we can use with the arrays to utilize them to the full extent:
Lbound: Returns the smallest available subscript for the indicated dimension of an array.
It will always return zero - so there is not much utility of the same
Ubound:Returns the largest available subscript for the indicated dimension of an array. This is most useful when working with the dynamic arrays.
Example
Dim arr()
For i=0 to 10
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
print ubound (arr)
Filter: we can use this to filter out the single dimension array. It returns a zero-based array containing a subset of a string array based on a specified filter criteria.If there are multiple values matching the filter criteria then filter will return the array according to the no of matching. If there are 4 values matched then it will return a single dimension array with the index as 3 i.e. arr(3)
Example:
Dim arr()
For i=0 to 10 step 2
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
For i=1 to 9 step 2
arr(i)= "Item" & i
Next
filteredArray=filter (arr,"Value")
Join: We can use this to join or concatenate the single dimension array values. It returns a string created by joining a number of substrings contained in an array.
Example:
Dim arr()
For i=0 to 10 step 1
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
print join(arr)

***********************************************************************************
Difference Between Arrays & Collections:
Although arrays are similar to collections, there is one primary difference. As a script writer, you have little control over collections. If you query WMI for a list of disk drives, you will get those disk drives back in whichever order WMI chooses. Furthermore, you will not be able to readily access individual drives without iterating through the entire collection.
By contrast, you can control the order of information in an array because you typically populate the array yourself. Furthermore, you have the ability to access individual items in an array without having to iterate through the entire set. This is because each item in an array is assigned an index number. In VBScript, the first item in an array is assigned index number 0, with subsequent items assigned index numbers 1, 2, 3, and so forth.
Arrays are of two types: One is fixed and another is dynamic.
Fixed arrays can be simply defined by using dim statement:
Dim arr(10)
this array can contain eleven values as array always start from 0
' Example:
Dim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
Another simple method can be as follows:
Dim arr
arr=Array(1,2,3,4,5,6,7,8,9,10)
This will define the array of size of 9 automatically having values from 1 to 10.
To Define dynamic arrays we will not specify the dimensions or length of the array when declaring the array
we will use the redim statement for specifying the dimensions like
Dim arr() ' this will initialize a array since we have not given dimensions in it we can not use it
ReDim arr (10) 'this will initialize the array to a dimension of 10 i.e. 11 elements.
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10 he
arr(i)= "Value" & i
Next
This will give you the same result
One drawback of this approach is that when you use the redim the values stored in the arrays will be lost:
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
ReDim arr(3)
when u will look into the array after the above code. The array will look like something shown below:

To overcome this we can use the Preserve keyword
Preserve: Preserves the data in an existing array when you change the size of the last dimension.
Example:
Dim arr()
ReDim arr(10)
For i=0 to 10
arr(i)= "Value" & i
Next
ReDim preserve arr(3)
Following are the list of functions that we can use with the arrays to utilize them to the full extent:
Lbound: Returns the smallest available subscript for the indicated dimension of an array.
It will always return zero - so there is not much utility of the same
Ubound:Returns the largest available subscript for the indicated dimension of an array. This is most useful when working with the dynamic arrays.
Example
Dim arr()
For i=0 to 10
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
print ubound (arr)
Filter: we can use this to filter out the single dimension array. It returns a zero-based array containing a subset of a string array based on a specified filter criteria.If there are multiple values matching the filter criteria then filter will return the array according to the no of matching. If there are 4 values matched then it will return a single dimension array with the index as 3 i.e. arr(3)
Example:
Dim arr()
For i=0 to 10 step 2
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
For i=1 to 9 step 2
arr(i)= "Item" & i
Next
filteredArray=filter (arr,"Value")
Join: We can use this to join or concatenate the single dimension array values. It returns a string created by joining a number of substrings contained in an array.
Example:
Dim arr()
For i=0 to 10 step 1
ReDim preserve arr(i)
arr(i)= "Value" & i
Next
print join(arr)

***********************************************************************************
Difference Between Arrays & Collections:
Although arrays are similar to collections, there is one primary difference. As a script writer, you have little control over collections. If you query WMI for a list of disk drives, you will get those disk drives back in whichever order WMI chooses. Furthermore, you will not be able to readily access individual drives without iterating through the entire collection.
By contrast, you can control the order of information in an array because you typically populate the array yourself. Furthermore, you have the ability to access individual items in an array without having to iterate through the entire set. This is because each item in an array is assigned an index number. In VBScript, the first item in an array is assigned index number 0, with subsequent items assigned index numbers 1, 2, 3, and so forth.
How we can assign a webelement inner text into an array?
ReplyDelete