| 1 | #include-Once |
|---|
| 2 | |
|---|
| 3 | ; #FUNCTION# ==================================================================================================================== |
|---|
| 4 | ; Name...........: _ArraySearch |
|---|
| 5 | ; Description ...: Finds an entry within a 1D or 2D array. Similar to _ArrayBinarySearch(), except that the array does not need to be sorted. |
|---|
| 6 | ; Syntax.........: _ArraySearch(Const ByRef $avArray, $vValue[, $iStart = 0[, $iEnd = 0[, $iCase = 0[, $iPartial = 0[, $iForward = 1[, $iSubItem = -1]]]]]]) |
|---|
| 7 | ; Parameters ....: $avArray - The array to search |
|---|
| 8 | ; $vValue - What to search $avArray for |
|---|
| 9 | ; $iStart - [optional] Index of array to start searching at |
|---|
| 10 | ; $iEnd - [optional] Index of array to stop searching at |
|---|
| 11 | ; $iCase - [optional] If set to 1, search is case sensitive. This is ignored if using regex comparison ($iCompare = 3). |
|---|
| 12 | ; $iCompare - [optional] 0 AutoIt variables compare (default), "string" = 0, "" = 0 or "0" = 0 match |
|---|
| 13 | ; 1 executes a partial search (StringInStr) |
|---|
| 14 | ; 2 comparison match if variables have same type and same value |
|---|
| 15 | ; 3 compares using a regular expression pattern provided as $vValue |
|---|
| 16 | ; $iForward - [optional] If set to 0, searches the array from end to beginning (instead of beginning to end) |
|---|
| 17 | ; $iSubItem - [optional] Sub-index to search on in 2D arrays |
|---|
| 18 | ; Return values .: Success - The index that $vValue was found at |
|---|
| 19 | ; Failure - -1, sets @error: |
|---|
| 20 | ; |1 - $avArray is not an array |
|---|
| 21 | ; |2 - $avArray is not a 1 or 2 dimensional array |
|---|
| 22 | ; |4 - $iStart is greater than $iEnd |
|---|
| 23 | ; |6 - $vValue was not found in array |
|---|
| 24 | ; |7 - $avArray has too many dimensions |
|---|
| 25 | ; Author ........: SolidSnake <MetalGX91 at GMail dot com> |
|---|
| 26 | ; Modified.......: gcriaco <gcriaco at gmail dot com>, Ultima - 2D arrays supported, directional search, code cleanup, optimization |
|---|
| 27 | ; BrunoJ - added compare option 3 for a regex pattern match |
|---|
| 28 | ; Remarks .......: This function might be slower than _ArrayBinarySearch() but is useful when the array's order can't be altered. |
|---|
| 29 | ; Related .......: _ArrayBinarySearch, _ArrayFindAll |
|---|
| 30 | ; Link ..........: |
|---|
| 31 | ; Example .......: Yes |
|---|
| 32 | ; =============================================================================================================================== |
|---|
| 33 | Func _ArraySearch(Const ByRef $avArray, $vValue, $iStart = 0, $iEnd = 0, $iCase = 0, $iCompare = 0, $iForward = 1, $iSubItem = -1) |
|---|
| 34 | If Not IsArray($avArray) Then Return SetError(1, 0, -1) |
|---|
| 35 | If UBound($avArray, 0) > 2 Or UBound($avArray, 0) < 1 Then Return SetError(2, 0, -1) |
|---|
| 36 | |
|---|
| 37 | Local $iUBound = UBound($avArray) - 1 |
|---|
| 38 | |
|---|
| 39 | ; Bounds checking |
|---|
| 40 | If $iEnd < 1 Or $iEnd > $iUBound Then $iEnd = $iUBound |
|---|
| 41 | If $iStart < 0 Then $iStart = 0 |
|---|
| 42 | If $iStart > $iEnd Then Return SetError(4, 0, -1) |
|---|
| 43 | |
|---|
| 44 | ; Direction (flip if $iForward = 0) |
|---|
| 45 | Local $iStep = 1 |
|---|
| 46 | If Not $iForward Then |
|---|
| 47 | Local $iTmp = $iStart |
|---|
| 48 | $iStart = $iEnd |
|---|
| 49 | $iEnd = $iTmp |
|---|
| 50 | $iStep = -1 |
|---|
| 51 | EndIf |
|---|
| 52 | |
|---|
| 53 | ; same var Type of comparison |
|---|
| 54 | Local $iCompType = False |
|---|
| 55 | If $iCompare = 2 Then |
|---|
| 56 | $iCompare = 0 |
|---|
| 57 | $iCompType = True |
|---|
| 58 | EndIf |
|---|
| 59 | |
|---|
| 60 | ; Search |
|---|
| 61 | Switch UBound($avArray, 0) |
|---|
| 62 | Case 1 ; 1D array search |
|---|
| 63 | If Not $iCompare Then |
|---|
| 64 | If Not $iCase Then |
|---|
| 65 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 66 | If $iCompType And VarGetType($avArray[$i]) <> VarGetType($vValue) Then ContinueLoop |
|---|
| 67 | If $avArray[$i] = $vValue Then Return $i |
|---|
| 68 | Next |
|---|
| 69 | Else |
|---|
| 70 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 71 | If $iCompType And VarGetType($avArray[$i]) <> VarGetType($vValue) Then ContinueLoop |
|---|
| 72 | If $avArray[$i] == $vValue Then Return $i |
|---|
| 73 | Next |
|---|
| 74 | EndIf |
|---|
| 75 | Else |
|---|
| 76 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 77 | If $iCompare = 3 Then |
|---|
| 78 | If StringRegExp($avArray[$i], $vValue) Then Return $i |
|---|
| 79 | Else |
|---|
| 80 | If StringInStr($avArray[$i], $vValue, $iCase) > 0 Then Return $i |
|---|
| 81 | EndIf |
|---|
| 82 | Next |
|---|
| 83 | EndIf |
|---|
| 84 | Case 2 ; 2D array search |
|---|
| 85 | Local $iUBoundSub = UBound($avArray, 2) - 1 |
|---|
| 86 | If $iSubItem > $iUBoundSub Then $iSubItem = $iUBoundSub |
|---|
| 87 | If $iSubItem < 0 Then |
|---|
| 88 | ; will search for all Col |
|---|
| 89 | $iSubItem = 0 |
|---|
| 90 | Else |
|---|
| 91 | $iUBoundSub = $iSubItem |
|---|
| 92 | EndIf |
|---|
| 93 | |
|---|
| 94 | For $j = $iSubItem To $iUBoundSub |
|---|
| 95 | If Not $iCompare Then |
|---|
| 96 | If Not $iCase Then |
|---|
| 97 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 98 | If $iCompType And VarGetType($avArray[$i][$j]) <> VarGetType($vValue) Then ContinueLoop |
|---|
| 99 | If $avArray[$i][$j] = $vValue Then Return $i |
|---|
| 100 | Next |
|---|
| 101 | Else |
|---|
| 102 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 103 | If $iCompType And VarGetType($avArray[$i][$j]) <> VarGetType($vValue) Then ContinueLoop |
|---|
| 104 | If $avArray[$i][$j] == $vValue Then Return $i |
|---|
| 105 | Next |
|---|
| 106 | EndIf |
|---|
| 107 | Else |
|---|
| 108 | For $i = $iStart To $iEnd Step $iStep |
|---|
| 109 | If $iCompare = 3 Then |
|---|
| 110 | If StringRegExp($avArray[$i][$j], $vValue) Then Return $i |
|---|
| 111 | Else |
|---|
| 112 | If StringInStr($avArray[$i][$j], $vValue, $iCase) > 0 Then Return $i |
|---|
| 113 | EndIf |
|---|
| 114 | Next |
|---|
| 115 | EndIf |
|---|
| 116 | Next |
|---|
| 117 | Case Else |
|---|
| 118 | Return SetError(7, 0, -1) |
|---|
| 119 | EndSwitch |
|---|
| 120 | |
|---|
| 121 | Return SetError(6, 0, -1) |
|---|
| 122 | EndFunc ;==>_ArraySearch |
|---|