Jump to content
Sign in to follow this  
blitzkrg

_ArraySearch() Error confusion

Recommended Posts

blitzkrg

Looking at the lastest help files it shows this for the results of an _ArraySearch()

Success:The first found instance of $vWhat2Find in $avArray

Failure:Returns -1 and sets @Error on Errors.

@Error=1 $avArray is not an array

@Error=2 $iStart is greater than UBound($AvArray)-1

@Error=3 $iEnd is greater than UBound($AvArray)-1

@Error=4 $iStart is greater than $iEnd

@Error=5 $iCaseSense was invalid. (Must be 0 or 1)

@Error=6 $vWhat2Find was not found in $avArray

but my results are -1 (indicating a failure) but the @Error returns is = 0

the help file only list 1-6 what is @error=0?

Edited by blitzkrg

Share this post


Link to post
Share on other sites
SmOke_N

I've not used this function, but '0' looks like it has found your pattern your searching for and returning the location.


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.

Share this post


Link to post
Share on other sites
blitzkrg

Thanks for the reply.

The function returns -1

which help file says -1 means a failure.

and then you can check what the error was by using the error codes.

and it gives error code 0

so to me (correct me if i'm wrong) the function would not return -1 if it had found what i was searching for right?

Share this post


Link to post
Share on other sites
blitzkrg

to work around this problem

i used _arraytostring and then a stringinstr to search my array

since _arraysearch() isnt working for me.

Share this post


Link to post
Share on other sites
SmOke_N

Well since you've been around for a while, let's take a look at why a -1 would be given.

Func _ArraySearch($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0, $iCaseSense = 0)
    Local $iCurrentPos, $iUBound
    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
    For $iCurrentPos = $iStart To $iEnd
        Select
            Case $iCaseSense = 0
                If $avArray[$iCurrentPos] = $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
            Case $iCaseSense = 1
                If $avArray[$iCurrentPos] == $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
        EndSelect
    Next
    SetError(6)
    Return -1
EndFunc

Ok, I don't see an @error that would throw a -1 here, however I do see a value of -1 that would be returned.

Any @error other than 0 would return a value of -1 here.

So:

$Pos = _ArraySearch ($Array, $Input)

If @error = 6 And $Pos = -1 Then MsgBox(0, "", "Not Found")

This is the same as

$Pos = _ArraySearch ($Array, $Input)

If $Pos = -1 Then MsgBox(0, "", "Not Found")

Except this covers @erros 1 thru 6 in general

But if you tried this:

$Pos = _ArraySearch ($Array, $Input)

If @error = -1 Then MsgBox(0, "", "Not Found")

It would fail, beause -1 isn't a setting for @error.

Hope that makes sense.

Edited by SmOke_N

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.

Share this post


Link to post
Share on other sites
blitzkrg

Ok, I don't see an @error that would throw a -1 here, however I do see a value of -1 that would be returned.

The @error doesnt throw a -1 it throws a 0

the function throws -1

Share this post


Link to post
Share on other sites
SmOke_N

Would help if I read a bit more eh.. :P You should not get a -1 if the @error returns 0 you should get the position of the found vairable in the array.... so you you are correct AFAIK.


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.

Share this post


Link to post
Share on other sites
blitzkrg

Would help if I read a bit more eh.. :P You should not get a -1 if the @error returns 0 you should get the position of the found vairable in the array.... so you you are correct AFAIK.

Ok - thanks for the confirmation.

Anybody know why this function is exhibiting odd behavior?

if it helps here is a bit of code that i'm using it for

$adds1 = GUICtrlRead($Input1)
    $adds4 = _FileListToArray($adds1)
    $adds5 = _ArraySearch($adds4,".mp3")

Now if i do a _displayarray ($adds4,"my array")

it will properly show me the array

so the array i'm trying to search is valid.

Share this post


Link to post
Share on other sites
SmOke_N

can you show what GUICtrlRead()'s actual value is?


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.

Share this post


Link to post
Share on other sites
SmOke_N

Here, I made my own... see if this helps you:

MsgBox(0, "", MyArraySearch(_FileListToArray(@DesktopDir), '.au3'))

Func MyArraySearch($MyArray, $Extension)
    Local $MakeArray
    For $i = 1 To UBound($MyArray) - 1
        If StringInStr($MyArray[$i], $Extension) Then 
            $MakeArray = $MakeArray & $MyArray[$i] & @LF
        EndIf
    Next
    Return $MakeArray
EndFunc

So for you code I would do:

$adds4 = _FileListToArray(GUICtrlRead($Input1))
MsgBox(0, "Test",  MyArraySearch($adds4, '.mp3'))

Func MyArraySearch($MyArray, $Extension)
    Local $MakeArray
    For $i = 1 To UBound($MyArray) - 1
        If StringInStr($MyArray[$i], $Extension) Then 
            $MakeArray = $MakeArray & $MyArray[$i] & @LF
        EndIf
    Next
    Return $MakeArray
EndFunc

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.

Share this post


Link to post
Share on other sites
blitzkrg

I was unable to get this working.

Instead i converted the Array to a string and just searched the string.

It was easier to type an extra line of code than to spend hours trying to get this 1 function to play nice.

I dont see any performance impact with going this route, so no big deal.

thanks 2 smoke_N for working on this with me.

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
Sign in to follow this  

×