Friday, September 9, 2011

VBScript Filter

VBScript Filter
Filter returns a Zero based array containing a subset of a string array based on criteria specified.

Syntax of Filter:
Filter(inputstrings,value[,include[,compare]])

inputstrings (A one-dimensional array of strings to be searched) and value (The string to search) are required and rest are optional.

If Include is True, Filter returns the subset of the array that contains Value as a substring. If Include is False, Filter returns the subset of the array that does not contain Value as a substring.

compare: Numeric value indicating the kind of string comparison to use (vbBinaryCompare or 0 and vbTextCompare or 1).
[if you have doubts about arrays, then please first polish your VBScript Arrays]

Example 1 of VBScript Filter
Dim MyArr
Dim O_Arr (3)
O_Arr(0) = "Apple"
O_Arr(1) = "Orange"
O_Arr(2) = "Banana"
MyArr = Filter(O_Arr, "A")
msgbox(MyArr(0)) 'Shows Apple

Example 2 of VBScript Filter
Dim MyArr
Dim O_Arr (3)
O_Arr(0) = "Apple"
O_Arr(1) = "Orange"
O_Arr(2) = "Banana"
MyArr = Filter(O_Arr, "A", False)
msgbox(MyArr(0)) 'Shows Apple
msgbox(MyArr(1)) 'Shows Banana

Example 3 of VBScript Filter
a=Array("Apple","Orange","Banana","Apricot","Guava","Grapes","Olive")
b=Filter(a,"A")
for each x in b
Msgbox(x)'will show Apple & Apricot.
next

Example 4 of VBScript Filter
a=Array("Apple","Orange","Banana","Apricot","Guava","Grapes","Olive")
b=Filter(a,"A", True)
for each x in b
Msgbox(x)'It shows all items containing 'A'.
next

Example 5 of VBScript Filter
a=Array("Apple","Orange","Banana","Apricot","Guava","Grapes","Olive")
b=Filter(a,"A", False)
for each x in b
Msgbox(x) 'it shows all items NOT containing 'A'.
next

Example 6 of VBScript Filter
a=Array("Apple","Orange","Banana","Apricot","Guava","Grapes","Olive")
b=Filter(a,"A", True,1)
for each x in b
Msgbox(x) 'Shows all items that contain A or a. For example it will not show Olive.
next

Example 7 of VBScript Filter
a=Array("Apple","Orange","Banana","Apricot","Guava","Grapes","Olive", "GUAVA")
b=Filter(a,"A", True,0)
for each x in b
Msgbox(x) 'Shows all items that contain A. Apple, Apricot, GUAVA
next


Also See:
Complete List of VBScript Array Functions