How to use ArrayLocate and restrict search?
;==========================================================================================================================================
;
;  How to use WinBatch function ArrayLocate()
;  in case of finding a name and a city for a particular id,
;  and restrict search result to specific index column?
;
;------------------------------------------------------------------------------------------------------------------------------------------
; Three examples in one script.
;
; 1. Example:
;    blnFirstResultOnly = @FALSE            ; Allow multiple results.
;    strMatchPattern    = "*" : @TAB : "*"  ; Search result is valid in all array dimensions, here it is Dim-1 and Dim-2.
;    Result: 3 hits.
;
; 2. Example:
;    blnFirstResultOnly = @FALSE            ; Allow multiple results.
;    strMatchPattern    = "*" : @TAB : "0"  ; Search result is only valid in the first column (0) of Dim-2 array.
;    Result: 2 hits.
;
; 3. Example:
;    blnFirstResultOnly = @TRUE             ; Allow only one result.
;    strMatchPattern    = "*" : @TAB : "0"  ; Search result is only valid in the first column (0) of Dim-2 array.
;    Result: 1 hit.
;
; 4. Example:
;    blnFirstResultOnly = @TRUE             ; Allow only one result.
;    strMatchPattern    = "*" : @TAB : "2"  ; Search result is only valid in the third column (2) of Dim-2 array.
;    Result: 1 hit.
;
; To test the examples simply comment out the prepared lines.
; Start with example 1.
;
; Note:
; The least significant array dimension can be incremented one past its maximum and ArrayLocate knows what you mean.
; It automagically adjusts all dimensions to start one element past the previously found element.
;
;------------------------------------------------------------------------------------------------------------------------------------------;
; (c)Detlev Dalitz.20100304.20100305.20110225.
;==========================================================================================================================================

DirChange (DirScript ())

;------------------------------------------------------------------------------------------------------------------------------------------
; Define the search array.

arrA = ArrDimension (25, 3)
intADims = ArrInfo (arrA, 0)
intADim1Max = ArrInfo (arrA, 1) - 1
intADim2Max = ArrInfo (arrA, 2) - 1

;------------------------------------------------------------------------------------------------------------------------------------------
; Set search array values for this example.

arrA[01, 0] = "WN001"     ; id
arrA[01, 1] = "Fred"      ; name
arrA[01, 2] = "Amsterdam" ; city

arrA[02, 0] = "WN002"     ; id
arrA[02, 1] = "Pete"      ; name
arrA[02, 2] = "WN007"     ; city

arrA[07, 0] = "WN007"     ; id
arrA[07, 1] = "Detlev"    ; name
arrA[07, 2] = "Wuppertal" ; city

arrA[11, 0] = "WN007"     ; id
arrA[11, 1] = "Detlev"    ; name
arrA[11, 2] = "Amsterdam" ; city

arrA[24, 0] = "WN024"     ; id
arrA[24, 1] = "John"      ; name
arrA[24, 2] = "New York"  ; city

;------------------------------------------------------------------------------------------------------------------------------------------
; Define the corresponding result array.

arrR = ArrDimension (intADims) ; The result array must have the same dimension like the search array.
intRDim1Max = ArrInfo (arrR, 1) - 1

;------------------------------------------------------------------------------------------------------------------------------------------
; Set result array values for the first start. Set all indexes to zero.

ArrInitialize (arrR, 0)

;------------------------------------------------------------------------------------------------------------------------------------------
; Define what to do.

strSearchItem = "WN007" ; The item that we seek.

; Example 4.
blnFirstResultOnly = @TRUE  ; Allow only one result. One hit.
strMatchPattern = "* 2"     ; The search result is only valid in the third column (2) of Dim-2 array.

; Example 3.
blnFirstResultOnly = @TRUE  ; Allow only one result. One hit.
strMatchPattern = "* 0"     ; The search result is only valid in the first column (0) of Dim-2 array.

; Example 2.
blnFirstResultOnly = @FALSE ; Allow multiple results.  Two hits.
strMatchPattern = "* 0"     ; The search result is only valid in the first column (0) of Dim-2 array.

; Example 1.
blnFirstResultOnly = @FALSE ; Allow multiple results. Three hits.
strMatchPattern = "* *"     ; The search result is valid in all array dimensions, here it is Dim-1 and Dim-2.


;------------------------------------------------------------------------------------------------------------------------------------------
; Do the search.

strListResults = ""

While @TRUE
   arrR = ArrayLocate (arrA, strSearchItem, arrR [0], arrR [1]) ; Search the Dim-2 array.
   If arrR [0] < 0 Then Break ; Search value is not found.

   strIndexes = "" ; Build a string of currently used array indexes for the matching.
   For intR = 0 To intRDim1Max
      strIndexes = strIndexes : " " : arrR[intR]
   Next
   strIndexes = StrSub (strIndexes, 2, -1)

   If StrIndexWild (strIndexes, strMatchPattern, 0) == 1 ; Are there any matches?
      If blnFirstResultOnly
         Break ; If first value is found, then break.
      Else
         ; Collect value or index into list or array. Here a string list is used to collect index pairs.
         strListResults = ItemInsert (arrR [0] : @TAB : arrR [1], -1, strListResults, @LF)
      EndIf
   EndIf

   arrR [intRDim1Max] = arrR [intRDim1Max] + 1 ; Next try. Increment the least significant array dimension of the result array.
EndWhile


;------------------------------------------------------------------------------------------------------------------------------------------
; Display result.

If arrR [0] < 0 && strListResults == ""
   strMsgTitle = "Result"
   strMsgText = "Nothing found."
   Pause (strMsgTitle, strMsgText)
Else
   If blnFirstResultOnly
      ; The values from the result array let us immediately pick the found item from the data array.
      strFoundItem = arrA[arrR[0], arrR[1]] ; Sure, we knew the item's value already, so this code line is not needed.
      strResult_ID   = arrA[arrR[0], 0] ; Once we know the array row of the found item, so in order ...
      strResult_Name = arrA[arrR[0], 1] ; ... to read values from the other columns too, ...
      strResult_City = arrA[arrR[0], 2] ; ... we have to address the columns as needed.
      strMsgTitle = "Single result"
      strMsgText = "Search item" : @LF : strSearchItem : @LF : @LF : "ID = " : strResult_ID : @LF : "Name = " : strResult_Name : @LF : "City = " : strResult_City
      Pause (strMsgTitle, strMsgText)
   Else
      strMsgTitle = "Multiple results"
      strMsgText = "Search item" : @LF : strSearchItem : @LF : @LF : "Array indexes" : @LF : strListResults : @LF : @LF : "Array values"
      intCount = ItemCount (strListResults, @LF)
      For intI = 1 To intCount
         intItem = ItemExtract (intI, strListResults, @LF)
         intIndex1 = ItemExtract (1, intItem, @TAB)
         strRow = "ID = " : arrA[intIndex1, 0] : @LF : "Name = " : arrA[intIndex1, 1] : @LF : "City = " : arrA[intIndex1, 2] : @LF
         strMsgText = ItemInsert (strRow, -1, strMsgText, @LF)
      Next
      Pause (strMsgTitle, strMsgText)
   EndIf
EndIf

:CANCEL
Exit
;==========================================================================================================================================