udfArrayFilter
arr udfArrayFilter (arr, str, bln, bln)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayFilter (arrArray, strFilter, blnInclude, blnIgnoreCase)
objSC = CreateObject ("MSScriptControl.ScriptControl")
objSC.Language = "vbscript"
objSC.AllowUI = @FALSE
Switch @TRUE
Case 256 == VarType (arrArray) ; WinBatch Array.
Case ObjectTypeGet (arrArray) == "ARRAY|VARIANT" ; Variant Array
   objSC.AddCode(: "Function ArrayFilter (Array)" : @LF : 'ArrayFilter=Filter(Array,"' : strFilter : '",' : blnInclude : ',' : blnIgnoreCase : ')' : @LF : "End Function")
   arrArray = objSC.Run(: "ArrayFilter", arrArray)
   Break
Case @TRUE
   objSC.AddCode(: "Dim a()")
   arrArray = objSC.Eval(: "a")
EndSwitch
Return arrArray
;..........................................................................................................................................
; This UDF "udfArrayFilter" returns a zero-based variant array that contains a subset of a string array based on a filter criteria.
;
; Parameters:
; arrArray ........ A one-dimensional array of strings to be searched. Array type can be WinBatch Array or Variant Array.
; strFilter ....... The string to search for.
; blnInclude ...... A boolean value that indicates whether to return the substrings that include or exclude value.
;                   @TRUE returns the subset of the array that contains value as a substring.
;                   @FALSE returns the subset of the array that does not contain value as a substring.
; blnIgnoreCase ... Specifies the string comparison to use.
;                   @TRUE performs a textual comparison (case ignored).
;                   @FALSE performs a binary comparison (case not ignored).
;..........................................................................................................................................
; Alternative codeline, see above:
; objVBSC.AddCode(: "Function ArrayFilter (Array)" : @LF : 'ArrayFilter = Filter(Array,"' : strFilter : '",' : ItemExtract (1 + blnInclude, "vbFalse,vbTrue", ",") : ',' : ItemExtract (1 + blnIgnoreCase, "vbBinaryCompare,vbTextCompare", ",") : ')' : @LF : "End Function")
;..........................................................................................................................................
; (c)Detlev Dalitz.20100120.20100122.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfDisplayArrayElements (arrArray)
intElements = ArrInfo (arrArray, 1)
If intElements > 0
   intElemLast = intElements - 1
   For intElem = 0 To intElemLast
      Message ("Array|Elements=" : intElements : "|Index=" : intElem, "Content=" : arrArray[intElem])
   Next
Else
   Message ("Array|Elements=" : intElements, "No elements ... nothing to do.")
EndIf
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; Example 1.
strFilterSearch = ""
blnFilterInclude = @TRUE
blnFilterIgnoreCase = @FALSE
Message ("WB", "Next example:" : @LF : 'Bad input parameter will cause empty variant array to return.')
arrFiltered = udfArrayFilter (0, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase) ; Result: Empty array.
udfDisplayArrayElements (arrFiltered) ; Result: "No Elements ... nothing to do."


; Create a WB Dim-1 array.
Message ("WB", "Next step:" : @LF : "Create WB array and display all elements in WB array.")
arrWB = ArrDimension (8)
arrWB[0] = "Holiday"
arrWB[1] = "Monday"
arrWB[2] = "Tuesday"
arrWB[3] = "Wednesday"
arrWB[4] = "Thursday"
arrWB[5] = "Friday"
arrWB[6] = "Saturday"
arrWB[7] = "Sunday"
udfDisplayArrayElements (arrWB)


; Example 2.
strFilterSearch = "n"
blnFilterInclude = @TRUE
blnFilterIgnoreCase = @TRUE
Message ("WB", "Next example:" : @LF : 'Find all elements in array which contain "' : strFilterSearch : '"' : @LF : "and return array with the result.")
arrFiltered = udfArrayFilter (arrWB, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase)
udfDisplayArrayElements (arrFiltered) ; Result: Monday, Wednesday, Sunday.


; Example 3.
strFilterSearch = "n"
blnFilterInclude = @FALSE
blnFilterIgnoreCase = @TRUE
Message ("WB", "Next example:" : @LF : 'Find all elements in array which do not contain "' : strFilterSearch : '"' : @LF : "and return array with the result.")
arrFiltered = udfArrayFilter (arrWB, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase)
udfDisplayArrayElements (arrFiltered) ; Result: Holiday, Tuesday, Thursday, Friday, Saturday.


; Example 4.
strFilterSearch = "H"
blnFilterInclude = @TRUE
blnFilterIgnoreCase = @TRUE
Message ("WB", "Next example:" : @LF : 'Find all elements in array which contain "' : strFilterSearch : '"' : @LF : "but ignore case and return array with the result.")
arrFiltered = udfArrayFilter (arrWB, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase)
udfDisplayArrayElements (arrFiltered) ; Result: Holiday, Thursday.


; Example 5.
strFilterSearch = "H"
blnFilterInclude = @TRUE
blnFilterIgnoreCase = @FALSE
Message ("WB", "Next example:" : @LF : 'Find all elements in array which contain "' : strFilterSearch : '"' : @LF : "but respect case and return array with the result.")
arrFiltered = udfArrayFilter (arrWB, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase)
udfDisplayArrayElements (arrFiltered) ; Result: Holiday.


; Example 6.
Message ("Ex. 6", "Next example:" : @LF : 'Set Holiday, Wednesday, Friday, Sunday to ObjectType "NULL"' : @LF : 'and shrink the array by removing the NULL elements.')
arrWB[0] = ObjectType ("NULL")
arrWB[3] = ObjectType ("NULL")
arrWB[5] = ObjectType ("NULL")
arrWB[7] = ObjectType ("NULL")

strFilterSearch = ObjectType ("NULL")
blnFilterInclude = @TRUE
blnFilterIgnoreCase = @TRUE
arrFiltered = udfArrayFilter (arrWB, strFilterSearch, blnFilterInclude, blnFilterIgnoreCase)
udfDisplayArrayElements (arrFiltered) ; Result: Monday, Tuesday, Thursday, Saturday.

Exit