Jump to content

Inconsistent Behavior _ArraySearch


Recommended Posts

During the course of my program it is possible for my array to have the value "0" as the only element stored. This array is still considered an array because IsArray returns TRUE. However, when I use _ArraySearch I don't get the expected "-1" for failure - rather I get "0" and @error isn't changed. Here is a quick example of the problem code:

#Include <Array.au3>

Dim $arTest[1] = [0]
ConsoleWrite(IsArray($arTest) & @CRLF & _ArraySearch($arTest,'test')& @CRLF & @error & @CRLF); output: 1 // 0 // 0, should be 1 // -1 // 6
$arTest[0] = 1
ConsoleWrite(_ArraySearch($arTest,'test') & @CRLF & @error & @CRLF); output: -1 // 6

While in my script it is easy enough to code for "If _ArraySearch($arTest,'test') <= 0 Then" because I have control of the arguments passed and know they are good. However, this doesn't bode well for error handling with this specific case.

Is there a valid reason for this inconsistent behavior?

Bob

You can't see a rainbow without first experiencing the rain.

Link to comment
Share on other sites

During the course of my program it is possible for my array to have the value "0" as the only element stored. This array is still considered an array because IsArray returns TRUE. However, when I use _ArraySearch I don't get the expected "-1" for failure - rather I get "0" and @error isn't changed. Here is a quick example of the problem code:

#Include <Array.au3>

Dim $arTest[1] = [0]
ConsoleWrite(IsArray($arTest) & @CRLF & _ArraySearch($arTest,'test')& @CRLF & @error & @CRLF); output: 1 // 0 // 0, should be 1 // -1 // 6
$arTest[0] = 1
ConsoleWrite(_ArraySearch($arTest,'test') & @CRLF & @error & @CRLF); output: -1 // 6

While in my script it is easy enough to code for "If _ArraySearch($arTest,'test') <= 0 Then" because I have control of the arguments passed and know they are good. However, this doesn't bode well for error handling with this specific case.

Is there a valid reason for this inconsistent behavior?

Bob

The behavior is due AutoIt use of the variant data type. When it does the comparison using '=' it determines the value of the array 0 to be numeric and then casts the value 'test' to a number which is also 0 so the comparison returns true. This will only happen with the value 0. If you set the _ArraySearch Case parameter to 1 it will force the use of == for the comparison which will cast both sides of the comparison to strings and will always give you the correct answer

Example:

Dim $arTest[1] = [0]
Dim $value = 'test'
$r = ($arTest[0] == $value)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $r = ' & $r & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$r = ($arTest[0] = $value)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $r = ' & $r & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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...