Jump to content
rodiney

search array

Recommended Posts

rodiney

hello

need help with array, I'm using search array to find a number stored in this array. example:

$ array [0] = 2

$ array [1] = 2

and I need to look for the number 2 within the array, I check the 2, 2 stored in the array, in my case it only finds the first array $ array [0] i need check two array.

Script.

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

Local $avArray[2]

$avArray[0] = 2
$avArray[1] = 2 
_ArrayDisplay($avArray, "$avArray")

Local $sSearch = 2

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

Share this post


Link to post
Share on other sites
czardas

Is this any help?

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

Local $aArray[4] = [2, 2, 0, 2] ; array to search
Local $vSearchWhat = 2

Local $iIndex = -1, $iItems = 0, $aResults[UBound($aArray)]

While 1
    $iIndex = _ArraySearch($aArray, $vSearchWhat, $iIndex +1)
    If $iIndex <> -1 Then
        $aResults[$iItems] = $iIndex
        $iItems += 1
    Else
        ExitLoop
    EndIf
WEnd
ReDim $aResults[$iItems]

_ArrayDisplay($aResults) ; indeces

; additional code
Local $sRet = ""
If UBound($aResults) = 0 Then
    MsgBox($MB_SYSTEMMODAL, "Not Found", '"' & $vSearchWhat & '" was not found')
Else
    For $i = 0 To UBound($aResults) -1 ; loop through the results
        $sRet &= $aResults[$i] & ", "
    Next
    $sRet = StringTrimRight($sRet, 2)
    MsgBox($MB_SYSTEMMODAL, "Found", '"' & $vSearchWhat & '" was found in the array at positions ' & $sRet)
EndIf

 

Edited by czardas
additional code
  • Like 1

Share this post


Link to post
Share on other sites
rodiney
4 hours ago, czardas said:

Is this any help?

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

Local $aArray[4] = [2, 2, 0, 2] ; array to search
Local $vSearchWhat = 2

Local $iIndex = -1, $iItems = 0, $aResults[UBound($aArray)]

While 1
    $iIndex = _ArraySearch($aArray, $vSearchWhat, $iIndex +1)
    If $iIndex <> -1 Then
        $aResults[$iItems] = $iIndex
        $iItems += 1
    Else
        ExitLoop
    EndIf
WEnd
ReDim $aResults[$iItems]

_ArrayDisplay($aResults) ; indeces

; additional code
Local $sRet = ""
If UBound($aResults) = 0 Then
    MsgBox($MB_SYSTEMMODAL, "Not Found", '"' & $vSearchWhat & '" was not found')
Else
    For $i = 0 To UBound($aResults) -1 ; loop through the results
        $sRet &= $aResults[$i] & ", "
    Next
    $sRet = StringTrimRight($sRet, 2)
    MsgBox($MB_SYSTEMMODAL, "Found", '"' & $vSearchWhat & '" was found in the array at positions ' & $sRet)
EndIf

 

I will test it.

Thank you

:)

Share this post


Link to post
Share on other sites
czardas

Easier than the above :lol: : you could simply use _ArrayFindAll(). Check the help file.

It's almost the same thing!

Edited by czardas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×