Function Reference


_ArraySearch

Finds an entry within a 1D or 2D array. Similar to _ArrayBinarySearch(), except that the array does not need to be sorted.

#include <Array.au3>
_ArraySearch(Const ByRef $avArray, $vValue [, $iStart = 0 [, $iEnd = 0 [, $iCase = 0 [, $iCompare= 0 [, $iForward = 1 [, $iSubItem = -1]]]]]])

Parameters

$avArray The array to search
$vValue What to search $avArray for
$iStart [optional] Index of array to start searching at
$iEnd [optional] Index of array to stop searching at
$iCase [optional] If set to 1, search is case sensitive
$iCompare [optional] 0 AutoIt variables compare (default), "string" = 0, "" = 0 or "0" = 0 match
1 executes a partial search (StringInStr)
2 comparison match if variables have same type and same value
$iForward [optional] If set to 0, searches the array from end to beginning (instead of beginning to end)
$iSubItem [optional] Sub-index to search on in 2D arrays

Return Value

Success: The index that $vValue was found at
Failure: -1, sets @error:
@error: 1 - $avArray is not an array
2 - $avArray is not a 1 or 2 dimensional array
4 - $iStart is greater than $iEnd
6 - $vValue was not found in array
7 - $avArray has too many dimensions
(3, 5 - Deprecated error codes)

Remarks

This function might be slower than _ArrayBinarySearch() but is useful when the array's order can't be altered.

Related

_ArrayBinarySearch, _ArrayFindAll

Example


#include <Array.au3>

;===============================================================================
; Example 1 (using a 1D array)
;===============================================================================
Local $avArray[6] = [ _
        "String0, SubString0", _
        "String1, SubString1", _
        "String2, SubString2", _
        "String3, SubString3", _
        "String4, SubString4", _
        "String5, SubString5"]

_ArrayDisplay($avArray, "$avArray")

Local $sSearch = InputBox("_ArraySearch() demo", "String to find?")
If @error Then Exit

Local $iIndex = _ArraySearch($avArray, $sSearch, 0, 0, 0, 1)
If @error Then
    MsgBox(0, "Not Found", '"' & $sSearch & '" was not found in the array.')
Else
    MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".")
EndIf

;===============================================================================
; Example 2 (using a 2D array)
;===============================================================================
Local $avArray[6][2] = [ _
        ["String0", "SubString0"], _
        ["String1", "SubString1"], _
        ["String2", "SubString2"], _
        ["String3", "SubString3"], _
        ["String4", "SubString4"], _
        ["String5", "SubString5"]]

_ArrayDisplay($avArray, "$avArray")

$sSearch = InputBox("_ArraySearch() demo", "String to find?")
If @error Then Exit

Local $sColumn = InputBox("_ArraySearch() demo", "Column to search?")
If @error Then Exit
$sColumn = Int($sColumn)

$iIndex = _ArraySearch($avArray, $sSearch, 0, 0, 0, 1, 1, $sColumn)
If @error Then
    MsgBox(0, "Not Found", '"' & $sSearch & '" was not found on column ' & $sColumn & '.')
Else
    MsgBox(0, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ' on column ' & $sColumn & '.')
EndIf