Jump to content

to be equal or not, that is my question


Go to solution Solved by Nine,

Recommended Posts

Global $var_A = "failed"
Global $var_B = "failed"
Global $var_C = "failed"

Global $default = @CRLF ; Null ; "" ; Default

Any_A(0) ; this always work
Func Any_A($val = $default)
    If Not($val == $default) Then $var_A = $val
EndFunc

Any_B(0) ; some times
Func Any_B($val = $default)
    If $val <> $default Then $var_B = $val
EndFunc

Any_C(0) ; never
Func Any_C($val = $default)
    If Not($val = $default) Then $var_C = $val
EndFunc

;~  -  $var_A = 0
;~  -  $var_B = failed
;~  -  $var_C = failed

ConsoleWrite('-  $var_A = ' & $var_A & @CRLF)
ConsoleWrite('-  $var_B = ' & $var_B & @CRLF)
ConsoleWrite('-  $var_C = ' & $var_C & @CRLF)

Should not <> mean != ?
Is this a bug, a fact of AutoIt, or just me not understanding it ?

Edit: Corrected the example

Edited by argumentum
oops

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

I suggest

  • to use Keyword "Default" to denote a default value
  • not to assign values to Global variables in a function

Could you please post a real life example so we can see what you try to achieve?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

34 minutes ago, argumentum said:

Func Any_C($val = $default)    

If Not($val = $default) Then $var_B = $val

EndFunc

$var_B should be $var_C here, no?:ermm:

Link to comment
Share on other sites

@argumentum  BTW : not a solution, just a typo :

[...]
Any_C(0) ; never
Func Any_C($val = $default)
    If Not($val = $default) Then $var_B = $val
EndFunc
[...]

If Not($val = $default) Then $var_C = $val -->not : If Not($val = $default) Then $var_B = $val

EDIT : Sorry, I had overlooked @RTFC 's post. There he already noted that typo :doh:.

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

4 hours ago, water said:

Could you please post a real life example

I corrected the example due to the wrong variable and to set the real life observation that got me to post. The default I was using is @CRLF. The brainstorm for that, is that I wanted to be able to pass a variable in the function to the "keyword Default", as the variable may use it. So is late at night and I'm like "I should use @CRLF as the default, it should be all the same" but @CRLF is way different to zero and <> should have catch that. But if that is so, what have I done since 2005 !!!, OMG !!!, ...I'll post and go to sleep. :D

"" and 0, ok. But @CRLF and 0, c'mon !. I know that an empty string and zero may get interpreted as nothing/zilch/nada, ok, zero. But a string ?. Hence the posting.

I've got code to get what I need. The question is, should a ticket be opened or not. Or am I still sleepy, hmm. :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Solution

Like @Subz already pointed out, it is not a good idea to voluntary compare String and Number.

Quote

Comparing different datatypes

Care is needed if comparing mixed datatypes, as unless the case-sensitive (==) string operator is used, mixed comparisons are usually made numerically. Most strings will be evaluated as 0 and so the result may well not be the one expected. It is recommended to force the items being compared into the same datatype using Number()/String() before the comparison.

 

Link to comment
Share on other sites

Here a detailed example how AutoIt works when comparing different datatypes :

; in this block, the string "string" is converted to number 0
ConsoleWrite((0 = "string") & @CRLF)
ConsoleWrite((1 = "string") & @CRLF)
ConsoleWrite((0 <> "string") & @CRLF & @CRLF)

; in this block, the string "01" is converted to number 1
ConsoleWrite((0 = "01") & @CRLF)
ConsoleWrite((1 = "01") & @CRLF)
ConsoleWrite((0 <> "01") & @CRLF & @CRLF)

; in this block, the numbers left to the == operator are first converted to number, then reconverted to string because of == op
ConsoleWrite((0 == "01") & @CRLF)
ConsoleWrite((1 == "01") & @CRLF)
ConsoleWrite((01 == "01") & @CRLF)
ConsoleWrite((010 == "10") & @CRLF)

 

Link to comment
Share on other sites

Right on spot!

I've submitted a revision of the help file to clarify that and other points. It shows in the beta help under Language Reference - Datatypes.

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)

Link to comment
Share on other sites

First one is easy --> false (because $a will be converted to 0)

Second one Not has highest precedence, so Not $a should return False, so False = 1 --> False 

Third one is easy if I am right on second --> True

ps.  my guess on second was right, but I wasn't really sure about it : gj Jocko

pss.  just googled translation on jocko : it means gaming.  You are wearing your name very well ! ;)

Edited by Nine
Link to comment
Share on other sites

17 hours ago, water said:

not to assign values to Global variables in a function

I would suggest that this is the very reason Global values exist. So that they can be accessed and assigned values from within a function and anywhere else in the program.

Help: "A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the global scope and can be read or changed from anywhere in the script."

Phil Seakins

Link to comment
Share on other sites

...is good advise nonetheless @pseakins. But the point was more along the use of the operators ( <>, =, etc. ). ( is a good read )
So as far as the OP question is concerned, it was satisfactorily answered. Shame on me for not being more of a ... programmer. I script without much thinking, to the point of not even taking datatypes into consideration. Gotta love AutoIt. Is very forgiving  :D

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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