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 $aArray, $vValue [, $iStart = 0 [, $iEnd = 0 [, $iCase = 0 [, $iCompare = 0 [, $iForward = 1 [, $iSubItem = -1 [, $bRow = False]]]]]]] )

Parameters

$aArray The array to search
$vValue What to search $aArray 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 Casting of variables to the same type (default), "string" = 0, "" = 0 or "0" = 0 match (If $iCase = 0)
    1 executes a partial search
    2 comparison match if variables have same type and same value
    3 compares using a regular expression pattern provided as $vValue
$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
$bRow [optional] If True then $iSubItem sets the row to search - False (default) searches columns

Return Value

Success: the index that $vValue was found at.
Failure: -1 and sets the @error flag to non-zero.
@error: 1 - $aArray is not an array
2 - $aArray is not a 1D or 2D array
3 - $aArray is empty
4 - $iStart is greater than $iEnd
5 - Array not 2D and $bRow set True
6 - $vValue was not found in array

Remarks

This function is likely to be slower than _ArrayBinarySearch() but is useful when the array cannot be sorted before the search.

The values of $iCompare can not be combined together.

Related

_ArrayBinarySearch, _ArrayFindAll

Example

Example 1

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $aArray[6][4]
For $i = 0 To 5
    For $j = 0 To 3
        $aArray[$i][$j] = "#" & $i & $j
    Next
Next
_ArrayDisplay($aArray, "Looking for '#32'", Default, 8)

; Search by column (looking in col 2)
Local $iIndex = _ArraySearch($aArray, "#32", 0, 0, 0, 0, 1, 2)
MsgBox($MB_SYSTEMMODAL, "Found '#32'", "Column 2 on Row " & $iIndex)

; Search by row (looking in row 3)
$iIndex = _ArraySearch($aArray, "#32", 0, 0, 0, 0, 1, 3, True)
MsgBox($MB_SYSTEMMODAL, "Found '#32'", "Row 3 in Col " & $iIndex)

Example 2

; using a 2D array

#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $avArray[6][2] = [ _
        ["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 $sColumn = InputBox("_ArraySearch() demo", "Column to search?")
If @error Then Exit
$sColumn = Int($sColumn)

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