Friday, September 14, 2012

VBScript - Working with Array

'Working with Array
' Dim keyword used to define array
' Redim keyword used to size or resize the arry
' Preserve optional- used to preserve the data in exisiting array, while you resize it
' Array Function
'Array(arglist) - use to define the array arguments
Dim arr1
arr1=Array("Sunday","Monday","Tuesday","Wedseday","Thursday","Friday","Saturday") ' first way of declare array with elements

'UBOUND() upperbound of array
msgbox "Upper bound of Array is " &UBOUND(arr1)
For i=0 to UBOUND(arr1)
        msgbox arr1(i)
Next

'LBOUND() lowerbound of array
msgbox "Lowerbound of Array is " &LBOUND(arr1)

'ISARRAY  function indicates whether a specific variable is array or not
Dim Var
Dim isarr(3) ' second way of declare array with given upper value and assign value to array elements
isarr(0)="one"
isarr(1)="Two"
isarr(2)="Three"
var=isarray(isarr)
msgbox "Isarr is variable is array or not" &var

Dim withoutupperlimitarr() ' declare array with no upper limit
var=IsArray(withoutupperlimitarr)
msgbox "withoutupperlimitarr is variable is array or not" &var

'Erase statement is used to empty array
Erase isarr
For i=0 to UBound(isarr)
    msgbox isarr(i)
Next

'Join() function joins substrings of an array into one long string with each substring separated by delimiter
'Join (list,[delimiter])
Dim a
Dim Joinex(3)
joinex(0)="This"
joinex(1)="is"
joinex(2)="join"
joinex(3)="example"
a=Join(joinex)
msgbox "Join string without delimiter" &a
a=join(joinex,",")
msgbox "Join string with delimiter" & a

'Split function returns a zero based , one dimenensional array containing a specified number of substring
'Split (expression, [,delimiter],count[,compare]) 0 means binary compare and 1 means text compare
Dim str,myarr, i
str="split * function* example"
myarr=split(str,"*",-1,1)
For i=0 to ubound(myarr)
    msgbox myarr(i)
Next

'Filter function returns zero based array containing a subset of a string array based on criteria specified
'Filter (inputstring, value,[,include[,compare])
'input string and value are required fields
' if include set to True return the subset of array contains the value as substring
' if include set to False then filter return the subset of the array that does not contains value as substring
a=Array("Apple","Orange","Banana","Olive","Apricoat")
b=filter(a,"A",True)
For each x in b
    msgbox "it shows all items containing A" &x
Next
c=filter(a,"A",False)
For each x in c
    msgbox "it shows all items NOT containing A" &x
Next
'Program to find the array elements start with A

No comments:

Post a Comment