udfIsItemInArray
bln udfIsItemInArray (arr, any)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsItemInArray (arrArray, anyItem)
Return udfArrayItemLocate (arrArray, anyItem) > -1
;..........................................................................................................................................
; This UDF "udfisItemInArray" returns a boolean value (@FALSE..@TRUE resp. 0..1)
; which indicates if a given Item is element of a given ascending sorted array.
; This UDF needs an ascending sorted array.
;
; This UDF calls another UDF "udfArrayItemLocate (arrArray, anyItem)".
;
; Detlev Dalitz.20020821.20090507.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayItemLocate (arrArray, anyItem)
If VarType (arrArray) != 256  Then Return -1 ; No array.
If ArrInfo (arrArray, 6) == 0 Then Return -1 ; No elements.
If ArrInfo (arrArray, 0) > 1  Then Return -1 ; Too much dimensions.
intTop = Max (0, ArrInfo (arrArray, 1) - 1)
intBot = 0
While (intTop - intBot - 1)
   intMid = (intTop + intBot) / 2
   If anyItem > arrArray [intMid] Then intBot = intMid
      Else intTop = intMid
EndWhile
If arrArray [intTop] == anyItem Then Return intTop
If arrArray [intBot] == anyItem Then Return intBot
Return -1
;..........................................................................................................................................
; This function "udfArrayItemLocate" uses the binary search algorithm
; to locate a given item in a given ascending sorted array.
; The function returns the index number of the found element,
; or returns -1 if the item was not found or the given array does not fit.
;
; The algorithm needs an ascending sorted array.
;
; Detlev Dalitz.20020821.20090507.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.
anyItem1 = ""        ;  0 ; Item is in array, element 1.
anyItem2 = "five"    ;  5 ; Item is in array, element 6.
anyItem3 = "zzzz"    ; 20 ; Item is in array, element 21.
anyItem4 = "abcd"    ; -1 ; Item is not in array.

; arrArray contains 21 elements, sorted ascending.
arrArray = Arrayize (",acht,drei,eight,eins,five,four,fuenf,neun,nine,one,sechs,seven,sieben,six,three,two,vier,zero,zwei,zzzz", ",")

blnResult1 = udfIsItemInArray (arrArray, anyItem1) ; 1 = @TRUE.
blnResult2 = udfIsItemInArray (arrArray, anyItem2) ; 1 = @TRUE.
blnResult3 = udfIsItemInArray (arrArray, anyItem3) ; 1 = @TRUE.
blnResult4 = udfIsItemInArray (arrArray, anyItem4) ; 0 = @FALSE.

Exit