Jump to content
Sign in to follow this  
Robjong

AutoIt strange behaviour?

Recommended Posts

Robjong

Hi,

If you try to check if a (non empty) string is equal to 0 it won't be ofcourse, but I noticed that when you try to check if a string is not equal 0 it actualy is... :D

Example:

; This works...
If "AutoIt" == 0 Then
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
Else
    ConsoleWrite("-> AutoIt is not 0" & @CRLF)
EndIf
; the above script outputs: -> AutoIt is not 0

; ...so I would expect this to work the same?
If "AutoIt" <> 0 Then
    ConsoleWrite("-> AutoIt is not 0" & @CRLF)
Else
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
EndIf
; the above script outputs: !> AutoIt is 0

Weird right?

edit: output comments

Edited by Robjong

Share this post


Link to post
Share on other sites
jvanegmond

There is no opposite operator to ==. The use of <> opposite to == is invalid. <> operator is opposite to =.

If you change your example, you will find:

If "AutoIt" = 0 Then
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
Else
    ConsoleWrite("-> AutoIt is NOT 0" & @CRLF)
EndIf

If "AutoIt" <> 0 Then
    ConsoleWrite("-> AutoIt is NOT 0" & @CRLF)
Else
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
EndIf

If "AutoIt" == 0 Then
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
Else
    ConsoleWrite("-> AutoIt is NOT 0" & @CRLF)
EndIf

If Not ("AutoIt" == 0) Then
    ConsoleWrite("-> AutoIt is NOT 0" & @CRLF)
Else
    ConsoleWrite("!> AutoIt is 0" & @CRLF)
EndIf

Share this post


Link to post
Share on other sites
jchd

Let me add a bit about this. Let's run the following:

ConsoleWrite("Condition 1: ('' = 0 ) is " & Execute(" ('' = 0 )") & @CRLF)
ConsoleWrite("Condition 2: ('' == 0 ) is " & Execute(" ('' == 0 )") & @CRLF)
ConsoleWrite("Condition 3: ('' = '0') is " & Execute(" ('' = '0')") & @CRLF)
ConsoleWrite("Condition 4: ('' == '0') is " & Execute(" ('' == '0')") & @CRLF)
ConsoleWrite("Condition 5: ('123' = 123) is " & Execute(" ('123' = 123)") & @CRLF)
ConsoleWrite("Condition 6: ('123' == 123) is " & Execute(" ('123' == 123)") & @CRLF)
ConsoleWrite("Condition 7: ('abc' = 0 ) is " & Execute(" ('abc' = 0 )") & @CRLF)
ConsoleWrite("Condition 8: ('abc' <> 0 ) is " & Execute(" ('abc' <> 0 )") & @CRLF)
ConsoleWrite("Condition 9: ('abc' == 0 ) is " & Execute(" ('abc' == 0 )") & @CRLF)
ConsoleWrite("Condition 10: Not ('abc' == 0 ) is " & Execute(" Not ('abc' == 0 )") & @CRLF)
ConsoleWrite("Condition 11: ('A' = 'a') is " & Execute(" ('A' = 'a')") & @CRLF)
ConsoleWrite("Condition 12: ('A' == 'a') is " & Execute(" ('A' == 'a')") & @CRLF)

We get:

Condition 1: ('' = 0 ) is True
Condition 2: ('' == 0 ) is False
Condition 3: ('' = '0') is False
Condition 4: ('' == '0') is False
Condition 5: ('123' = 123) is True
Condition 6: ('123' == 123) is True
Condition 7: ('abc' = 0 ) is True
Condition 8: ('abc' <> 0 ) is False
Condition 9: ('abc' == 0 ) is False
Condition 10: Not ('abc' == 0 ) is True
Condition 11: ('A' = 'a') is True
Condition 12: ('A' == 'a') is False

A few comments now.

There are various cases:

Conditions 3, 4: here we're just comparing clearly distinct strings here, so no surprise.

Conditions 8, 10: are obviously the inverse of conditions 7 and 9, resp. (only as reference to the OP)

Conditions 11, 12: == is case sensitive, while = is not, so no surprise either.

Conditions 1, 2, 7, 9: more interesting in that an empty or non-numeric string = 0 but Not == 0

Conditions 5, 6: also interesting in that a numeric string is always (= or ==) the same that its Number()

This is for illustration only, I see nothing buggy here. This behavior is simply a given that need to have in mind while coding.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
MvGulik
whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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.