Jump to content

Unexpected behavior of = equals operator conditionals


Recommended Posts

AutoIt documentation of operations states, for the '=' operator,

= Tests if two values are equal.  e.g. If $var= 5 Then    (true if $var equals 5). Case insensitive when used with strings.

 

However, I have found that when the value to compare to is 0, all string values compare as equal to 0.

For example,

If("Foo" = 0) Then MsgBox(1, "Result", "Apparently, Foo = 0")

Also true for >= and <=

This behavior does not occur with the '==' operator.

Local $a="Foo", $b=0, $res=""
If("Foo" = 0) Then $res&="Apparently Foo = 0"&@CRLF
If ($a = $b) Then $res&="Apparently "&$a&" = "&$b&@CRLF
If ($a == $b) Then $res&="Apparently "&$a&" == "&$b&@CRLF
If ($a <= $b) Then $res&="Apparently "&$a&" <= "&$b&@CRLF
If ($a >= $b) Then $res&="Apparently "&$a&" >= "&$b&@CRLF
If ($a < $b) Then $res&="Apparently "&$a&" < "&$b&@CRLF
If ($a > $b) Then $res&="Apparently "&$a&" > "&$b&@CRLF
$b=1
If ($a = $b) Then $res&="Apparently "&$a&" = "&$b&@CRLF
If ($a == $b) Then $res&="Apparently "&$a&" == "&$b&@CRLF
MsgBox(1, "Results", $res)
Link to comment
Share on other sites

According to the help file:

"If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

ConsoleWrite(Number("test") & @CRLF)
ConsoleWrite(IsBool("test") & @CRLF)
if "test" Then
    ConsoleWrite("should not display" & @CRLF)
Else
    ConsoleWrite("should display" & @CRLF)
EndIf





 output:

0

0

should not display

the bool operator acts differently then?

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

jdelaney,

AutoIt treats empty strings as Boolean False and with any content as True. So what you get there seems entirely logical to me: :)

IsNum -> It is not so 0
IsBool -> It is not so 0
If "test" -> It is not "" and so is taken as True

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

According to the help file:

"If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0."

 

I see. So the documentation for '==' is clear that the values are converted to strings, but the documentation of '=' does not make it clear that the values are converted to numbers. So I agree that if the documentation is updated, then this is expected behavior.

Link to comment
Share on other sites

"If a string is used as a boolean and it is  an empty string "" , it will be assumed to equal False (see below).  For example,

    NOT "" equals the Boolean true."

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I see. So the documentation for '==' is clear that the values are converted to strings, but the documentation of '=' does not make it clear that the values are converted to numbers. So I agree that if the documentation is updated, then this is expected behavior.

 

Actually, I'm going to retract that, since the documentation for '=' says that it compares string in a case-insensitive manner, so it seems to matter whether only one value is a string (string is converted to number) or both are strings (strings remain strings).

Link to comment
Share on other sites

  • Moderators

mwhidden,
 

the documentation of '=' does not make it clear that the values are converted to numbers

That is because it is not the case. ;)

You can compare 2 strings with the "=" operator and they both remain strings, but are not compared case-sensitively (if such a word exists):

If "Test" = "test" Then
    ConsoleWrite("True" & @CRLF)
Else
    ConsoleWrite("False" & @CRLF)
EndIf

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

mwhidden,

 

That is because it is not the case. ;)

You can compare 2 strings with the "=" operator and they both remain strings, but are not compared case-sensitively (if such a word exists):

If "Test" = "test" Then
    ConsoleWrite("True" & @CRLF)
Else
    ConsoleWrite("False" & @CRLF)
EndIf

M23

 

I'm responding to user "water" who stated that in my example, the string were being implicitly converted to numbers. Are you saying that user water is incorrect in his explanation of why "Foo" = 0 is true but "Foo" == 0 is false?

I'm still unclear as to whether and when '=' and '==' converts a string to a number. It seems that '=' does, at least sometimes.

Link to comment
Share on other sites

If one part of a comparison using "=" is a number then the other part is implicitely converted to a number.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If one part of a comparison using "=" is a number then the other part is implicitely converted to a number.

 

If that's true, I'm having trouble finding where that is documented. I can't find (I might just be missing it) information on type coercion, or perhaps the documentation could make that more clear. It is true that "Foo" = 0 and that  NOT "Foo" == 0. The latter is clear from the documentation, but the former is not clear (not to me) until it bites you.

Link to comment
Share on other sites

  • Moderators

mwhidden,

If you compare 2 like data-types then things are pretty simple - except that using "==" forces both sides to strings regardless of their type, so you might get an unexpected result which is why we do not recommend using that operator except for case-sensitive string comparison. If you mix the data-types that you can get into all sorts of trouble when "=" does not return what you think it should. So the top tip is not to mix data-types in a comparison. :)

In the OP you were comparing a string ("Foo") to a number (0) - AutoIt will then convert both to a number (I do not have access to the code but experience tells me this is usually the case). As I stated above - do not mix data-types in comparisons - then you control how the comparison actually compares.  This "bug" raises its head from time to time - it is one of the down-sides of having a non-typed language, but personally I feel that it worth it. All clear now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

If that's true, I'm having trouble finding where that is documented. I can't find (I might just be missing it) information on type coercion, or perhaps the documentation could make that more clear. It is true that "Foo" = 0 and that  NOT "Foo" == 0. The latter is clear from the documentation, but the former is not clear (not to me) until it bites you.

Help file Language Reference=>Datatypes

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

Link to comment
Share on other sites

mwhidden,

If you compare 2 like data-types then things are pretty simple - except that using "==" forces both sides to strings regardless of their type, so you might get an unexpected result which is why we do not recommend using that operator except for case-sensitive string comparison. If you mix the data-types that you can get into all sorts of trouble when "=" does not return what you think it should. So the top tip is not to mix data-types in a comparison. :)

In the OP you were comparing a string ("Foo") to a number (0) - AutoIt will then convert both to a number (I do not have access to the code but experience tells me this is usually the case). As I stated above - do not mix data-types in comparisons - then you control how the comparison actually compares.  This "bug" raises its head from time to time - it is one of the down-sides of having a non-typed language, but personally I feel that it worth it. All clear now? :)

M23

 

 I was confused because in an earlier post you stated that '=' does not convert values to numbers, but I guess you meant that it does not convert both to numbers, but sometimes converts only one to a number.

But, now that I know that '=' (and also <, >, <=, >=) coerces strings to numbers in mixed-type situations ( vs. the other way around), I can be wary in the future. Is this the proper forum to request an update to the documentation?

Link to comment
Share on other sites

  • Moderators

mwhidden,

 

in an earlier post you stated that '=' does not convert values to numbers

No, I said that your assertion "that the values are converted to numbers" was not true. And it is not true in all cases, as I explained. :)

If you want to suggest a change to the documentation - then provide some suitable wording to replace/add to what is already there and post in this thread. Whether guinness accepts it is, of course, another matter. ;)

M23

P.S. When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unneccessarily. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Help file Language Reference=>Datatypes

 

Thank you, but sorry it is not there. It says that Strings are coerced to numbers (using Number()) when strings are used, but there is no rule for when strings are treated as numbers.

Common sense indicates that mathematical operators like +,-, etc., would treat operands as numbers, and the exampls in teh documentation imply this is so. However, there is nothing that speaks to the comparison operations, except in the case of ==.

Empirical evidience suggests that comparison operators (except for == and <>) treat operands as numbers if at least one operand is a number. Otherwise, they treats the operands as strings.

Link to comment
Share on other sites

There is a >thread where you can report help file changes. But before posting, check that the issue hasn't already been sobved.

Edit: Too late.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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