Jump to content
Sign in to follow this  
Juvigy

_arraysearch doesnt work when using 0 as search parameter

Recommended Posts

Juvigy

Global $groups[3] = ["asdsad","asdasdasdasd","asddsadasdasd"]
MsgBox(0,_ArraySearch($groups,1),1)
MsgBox(0,_ArraySearch($groups,0),0)

First msgbox return -1 as expected but second one returns 0

I dont know if this is corrected in the new versions.The issue is rooted here:

Switch UBound($avArray, 0)
        Case 1 ; 1D array search
            If Not $iPartial Then
                If Not $iCase Then
                    For $i = $iStart To $iEnd Step $iStep
                        If $avArray[$i] = $vValue Then Return $i ;in this line if you replace = with == it works
                    Next
                Else
                    For $i = $iStart To $iEnd Step $iStep
                        If $avArray[$i] == $vValue Then Return $i
                    Next
                EndIf
            Else
                For $i = $iStart To $iEnd Step $iStep
                    If StringInStr($avArray[$i], $vValue, $iCase) > 0 Then Return $i
                Next
            EndIf

@edit - fixed a copy error

Edited by Juvigy

Share this post


Link to post
Share on other sites
seandisanti

Global $groups[3] = ["asdsad","asdasdasdasd","asddsadasdasd"]
MsgBox(0,_ArraySearch($groups,1),1)
MsgBox(0,_ArraySearch($groups,1),0)

First msgbox return -1 as expected but second one returns 0

um... you're only referencing the array search in the title portion of your message box... the second one is returning 0 because you're telling it to... both messageboxes have -1 as the title...

Share this post


Link to post
Share on other sites
Juvigy

just a copy error - i will fix it but the issue is still here.

Share this post


Link to post
Share on other sites
Tvern

You're comparing a string to a number. The numeric representation of a non-number string is 0, so this is not a bug.

If you search for the string "0" it returns -1 (@error = 6) as expected.

Also your fix would make it impossible to do a case-insensitive search

Edit: Reading that back I don't think it's the best explanation I've ever seen. Have a look at datatypes in the helpfile it I'm unclear.

Edited by Tvern

Share this post


Link to post
Share on other sites
Juvigy

Yes i get it.But this is not added to the help file and documentation.It is quite tricky to catch if you dont know it and work with arrays.

I think modification of the function is needed to cover this - maybe conversion of number to string or something like this.

Share this post


Link to post
Share on other sites
Tvern

Datatypes are explained in the helpfile.

Because (almost?) all functions require, or return data of a certain type it would make little sense to only add a link to that page for _ArraySearch, nor would it make much sense to add the same link to every function in the helpfile.

I think it's seen as basic knowledge everyone using AutoIt should know about. (but don't quote me on this)

Allot of bug reports seem to be caused by cross-type comparisons though.

Share this post


Link to post
Share on other sites
Juvigy

Indeed and as you cant explicitly say what datatype your array would be when declaring and as i think it can hold more than 1 type it is a good point to make sure arraysearch is a little bit more fool-prove.Even it doesnt take too much effort to fix this.I personally think that strings should not be evaluated as 0 or -1 but as something else - the developers should advise on that.

Share this post


Link to post
Share on other sites
seandisanti

Indeed and as you cant explicitly say what datatype your array would be when declaring and as i think it can hold more than 1 type it is a good point to make sure arraysearch is a little bit more fool-prove.Even it doesnt take too much effort to fix this.I personally think that strings should not be evaluated as 0 or -1 but as something else - the developers should advise on that.

in the udf's that you write and document; do you do a rewrite of the udf and documentation every time someone finds a way to make it behave in a way that they disagree with because they call it with bad parameters? i definitely don't, i show them the right way to use it, maybe explain why, and then tell them to use it right. it operates as described when used as instructed and adheres to the conventions of the language, just be careful with your function calls, or you could just write your own udf to do the same thing except the way that you want it done...

Share this post


Link to post
Share on other sites
Juvigy

"Call it with bad parameters?"

Nowhere in the documentation in the function it doesnt say about what should be the parameters - even so - errors happen and all functions should be as much documented and fool proof as possible.And yes - i would rewrite this function - i already did.

Share this post


Link to post
Share on other sites
Spiff59

I've repeatedly suggested that the true differences between the "=" and "==" operators ought to be documented:

Shouldn't this behavior be documented?

Feature request (documentation)?

Value of 0 = "" ?

Something as basic and critical to a language as how comparison operators behave, ought to be fully described.

My personal opinion regarding this specific thread is that, if an _ArraySearch() function is called with zero as the search parameter, and it returns a false positive, then the function is obviously bugged.

Edit: Adding this one line to the edits at the top of the _ArraySearch() function corrects this problem (and a similar problem when searching for an empty array element):

If $vValue == 0 Or $vValue == "" Then $iCase = 1
Edited by Spiff59

Share this post


Link to post
Share on other sites
BrewManNH

I am looking at the example you gave and I don't even see what it is you're trying to search for. This code

MsgBox(0,_ArraySearch($groups,1),1)

is searching the array $groups for the number 1, you don't have a number 1 in any of the array elements, so you're going to get a -1 in the title, and the MsgBox text will only EVER show the number 1. I don't think you know how _ArraySearch works from the most basic functionality of it. _ArraySearch searches the array for whatever you put into the second parameter, and it will return the array element that holds that parameter. So if you rewrote your example to this:

MsgBox(0,_ArraySearch($groups,"asddsadasdasd"),1)

Then the msgbox title would show 2, the text would still display 1.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Spiff59

I believe he is aware of how _ArraySearch functions, and should function, although his demo script could be better documented. His first call to _ArraySearch() is simply a demonstration that it works correctly when the search parm is a non-zero numeric. His second call shows that the function fails if the parameter is 0.

Edited by Spiff59

Share this post


Link to post
Share on other sites
BrewManNH

I believe he is aware of how _ArraySearch functions, and should function, although his demo script could be better documented. His first call to _ArraySearch() is simply a demonstration that it works correctly when the search parm is a non-zero numeric. His second call shows that the function fails if the parameter is 0.

Actually, his examples both return the exact same results, run them and you'll see that the MsgBox title is ALWAYS -1 only the text changes, because it's implicitly set to display 1 and 0 in the 2 examples.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Spiff59

I think his point would have been clearer had he written the demo more like:

Global $groups[3] = ["asdsad","asdasdasdasd","asddsadasdasd"]
MsgBox(0,"Result = " & _ArraySearch($groups,1),"Should be -1")
MsgBox(0,"Result = " & _ArraySearch($groups,0),"Should be -1")

I'm curious as to what will be changed since BugTracker #1569 shows a status of "Completed". Maybe this will be corrected in 3.3.7.0...

Edited by Spiff59

Share this post


Link to post
Share on other sites
PsaltyDS

The resolution explicitly says: "Added by revision [5854] in version: 3.3.7.0"

It may be a while before that version is released, however.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.