Jump to content

Modified _ArraySearch()


GEOSoft
 Share

Recommended Posts

I have a situation where I need to perform a reverse search on an array but the array can't be modified. So I can't use _ArrayReverse().

I modified SolidSnake's origional to do it. Here it is with the new code commented and a demo. I'm going to submit this for replacement in Array.au3

Edit: It was pointed out that I somehow changed the parameter order and it would have broken existing scripts. This code has now been corrected

$TArray = StringSplit("Array|Color|Date||Dialog|File|Folder|GuiCombo|" & _
"GuiEdit|GuiIPAddress|GuiList|GuiListView|GuiMonthCal|GuiSlider|GuiStatusBar|" & _
"GuiTab|GuiTreeView|IE|Inet|Ini|Language|Math|Memory|Misc|Process", '|')
$aSrch = "GUI"
 $first = _ArraySearch( $TArray, $aSrch, 0, 0, 0,true);; Search forwards
 $last = _ArraySearch( $TArray, $aSrch, 0, 0, 0,true, 1);; Search  backwards
 MsgBox(0,'Test _ArraySearch()', "First = " & $first & @CRLF & "Last = " & $last)

Func _ArraySearch(Const ByRef $avArray, $vFind, $iStart = 0, $iEnd = 0, $iCaseSense = 0, $fPartialSearch = False, $iSearchForward = 0)
;;<<======  Added the [$iSearchForward = 0] parameter and changed $vWhat2Find to the shorter $vFind
    Local $iCurrentPos, $iUBound, $iResult, $iStep = 1, $iBegin, $iStop;; <<===  Added , $iStep = 1, $iBegin, $iStop
    If Not IsArray($avArray) Then
        SetError(1)
        Return -1
    EndIf
    $iUBound = UBound($avArray) - 1
    If $iEnd = 0 Then $iEnd = $iUBound
    If $iStart > $iUBound Then
        SetError(2)
        Return -1
    EndIf
    If $iEnd > $iUBound Then
        SetError(3)
        Return -1
    EndIf
    If $iStart > $iEnd Then
        SetError(4)
        Return -1
    EndIf
    If Not ($iCaseSense = 0 Or $iCaseSense = 1) Then
        SetError(5)
        Return -1
    EndIf
#Region ## <<==========  Start New Code
    If Not ($iSearchForward = 0 Or $iSearchForward = 1) Then
        SetError(7)
        Return -1
    EndIf
    If $iSearchForward = 1 Then
        $iBegin = $iEnd
        $iStop = $iStart
        If $iStop = $iUbound Then $iStop = 0
        $iStep = -1
    Else
    $iBegin = $iStart
    $iStop = $iEnd
    $iStep = 1
    EndIf
#EndRegion;;<<======  End New Code
;For $iCurrentPos = $iStart To $iEnd;;<<======  Changed to the following line
    For $iCurrentPos = $iBegin To $iStop Step $iStep
        Select
            Case $iCaseSense = 0
                If $fPartialSearch = False Then
                    If $avArray[$iCurrentPos] = $vFind Then
                        SetError(0)
                        Return $iCurrentPos
                    EndIf
                Else
                    $iResult = StringInStr($avArray[$iCurrentPos], $vFind, $iCaseSense)
                    If $iResult > 0 Then
                        SetError(0)
                        Return $iCurrentPos
                    EndIf
                EndIf
            Case $iCaseSense = 1
                If $fPartialSearch = False Then
                    If $avArray[$iCurrentPos] == $vFind Then
                        SetError(0)
                        Return $iCurrentPos
                    EndIf
                Else
                    $iResult = StringInStr($avArray[$iCurrentPos], $vFind, $iCaseSense)
                    If $iResult > 0 Then
                        SetError(0)
                        Return $iCurrentPos
                    EndIf
                EndIf
        EndSelect
    Next
    SetError(6)
    Return -1
EndFunc;==>_ArraySearch
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators
:) I don't even remember writing that ;)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

:) I don't even remember writing that ;)

That's my mistake. You didn't write it. My appologies to SolidSnake who did write it though.

Note to self. You are about to burn out. ABORT LIFE!!!!

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...