Cusem

= and ==

8 posts in this topic

This might be a n00bish question, but I just spent 2 hours figuring out why the (test) message box would keep popping up with the following code:

Dim $aNote[5] = [20090726, "Title of note", 20090703, 7, 342345]

....

$INP_NOTES_title = GUICtrlCreateInput($aNote[1], 50, 50, $GUINotes_Width - 240, 30, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
If $aNote[1] = 0 Then 
    MsgBox(0, "test", $aNote[1])
    ;GuiCtrlSetData($INP_NOTES_title, "No Title")
EndIf

The title was set correctly in the GuiCtrlCreateInput function, but for some reason "If $aNote[1] = 0" seemed to be true, since the msgbox (created for test purposes) kept popping up and the (to me) baffling thing is, that the test message box would display "Title of note" every time (instead of "0" after just saying $aNote[1] = 0 = true), while 1 line ago that variable would equal 0.

After 2 hours of looking at the monitor in a very perculiar way, I figured out that If $aNote[1] == 0 Then (double = ) was NOT true while a single = would be true.

Could anybody explain what the difference is? I read the "Operators" section in the helpfile and I figure == is for making comparisons if 2 values are equal and = is for assigning values to variables. I've never used == in my scripts however and never ran into any problem regarding this matter. Why would the simple code posted above do create a problem?

Share this post


Link to post
Share on other sites



That's because "Title of note" has no numeric value. = compared two numbers. == is for comparing case sensitive strings. == is forcing string comparison so 0 gets converted to "0".

When you know you're comparing strings, you ought to compare it to a string value and not a number.

Share this post


Link to post
Share on other sites

This might be a n00bish question, but I just spent 2 hours figuring out why the (test) message box would keep popping up with the following code:

Dim $aNote[5] = [20090726, "Title of note", 20090703, 7, 342345]
 
 ....
 
 $INP_NOTES_title = GUICtrlCreateInput($aNote[1], 50, 50, $GUINotes_Width - 240, 30, BitOR($ES_CENTER,$ES_AUTOHSCROLL))
 If $aNote[1] = 0 Then 
     MsgBox(0, "test", $aNote[1])
     ;GuiCtrlSetData($INP_NOTES_title, "No Title")
 EndIf

The title was set correctly in the GuiCtrlCreateInput function, but for some reason "If $aNote[1] = 0" seemed to be true, since the msgbox (created for test purposes) kept popping up and the (to me) baffling thing is, that the test message box would display "Title of note" every time (instead of "0" after just saying $aNote[1] = 0 = true), while 1 line ago that variable would equal 0.

After 2 hours of looking at the monitor in a very perculiar way, I figured out that If $aNote[1] == 0 Then (double = ) was NOT true while a single = would be true.

Could anybody explain what the difference is? I read the "Operators" section in the helpfile and I figure == is for making comparisons if 2 values are equal and = is for assigning values to variables. I've never used == in my scripts however and never ran into any problem regarding this matter. Why would the simple code posted above do create a problem?

If you compare a string to a number then the string is first converted to a number. In this case it is zero but had the string been just digits then it would evaluate to the number represented by the digits. Ie "123" = 123 is true.

If you use double equal signs, the AutoIt gives true if the two items being compared are exactly the same. Since a string is not an integer it gives false.

It's just the way AutoIt is designed and when you know that it can be a very useful characteristic.

Had you written

If $aNote[1] = "0" Then

then you might have got what you expected.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

First: mixing numbers and strings inside the same array = BAAAD idea, AutoIt will become confused about what is inside that array, string of chars or numbers? and you'll get results like you got.

Second: = means "equal" and == means "identical"

It might look hilarious but it is not.

You had the value 20090726 which was treated like a string because of "Title of note" present in your array ("20090726"). You were comparing numerical values $aNote[1] = 0 (number 0); since AutoIt found a string in position [1] it's numerical value it's 0 and that was why the condition was true.

The following statements can do the trick and make a good comparison:

-comparing 2 numbers

If Number($aNote[1]) = 0 Then

-comparing 2 text values

If $aNote[1] = "0" Then

-forcing comparation between 2 values

If $aNote[1] == 0 Then

Hope this explanation is making the whole thing clear >_<


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Yep it does! Thanks for the quick info guys!

Strange this is the first time I ran into this problem though, since I never used "==" before and have used a lot of arrays containing strings and numbers.

When posting the first post I figured I had to make it a habit to use "=" when I assign variables and "==" when I want to check if 2 values are equal, but I guess I had this wrong.

Is it: [Always use "=", except for when you're checking if 2 strings are equal, in that case use "=="]?

Share this post


Link to post
Share on other sites

== is case sensitive comparison. It forces both variables to string type to do this.

= is a little ambiguous since AutoIt uses variants.

It's easier to just not much comparing strings and numbers.

Share this post


Link to post
Share on other sites

So what's the best practice to use?

I'm used to == for comparing and = only to assign (and === for identical comparisons, means types are the same), is it bad if I always use == to compare in autoit? [And I mean really bad, because if I get used to = to compare I'll make bad errors in other languages, e.g. where you can assign values in if clauses.]

Share this post


Link to post
Share on other sites

@blk_panther take a look in the help file under Operators. It shows in very short order when to use = for comparison and when to use ==.


√-1 2^3 ∑ π, and it was delicious!

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