Sign in to follow this  
Followers 0
dark_jedi

string and number comparison

10 posts in this topic

#1 ·  Posted (edited)

how do string and number comparisons work such as

$a="foobar"
$b=1

if not $a==$b Then MsgBox(0,"no","foobar does not==1")
if $a==$b Then MsgBox(0,"yes","foobar==1")

which by the way when i run the script neither if statement gets executed which seems impossible.

Edited by dark_jedi

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$a="foobar"
$b=1

if $a==$b Then 
MsgBox(0,"","Matched")
else
MsgBox(0,"","Didnt Match")
endif
 oÝ÷ ÚÚºÚ"µÍÌÍØOI][ÝÙÛØ][ÝÂÌÍØLBY   ÌÍØIÉÝÉÌÍØ[ÙÐÞ
    ][ÝÉ][ÝË    ][ÝÑYÌÎNÝX]Ú  ][ÝÊB[ÙBÙÐÞ
    ][ÝÉ][ÝË    ][ÝÓX]ÚY ][ÝÊB[Y

<> (not equal to) is for comparisons

guess i don't know exactly what you are asking

Edited by death pax

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

first of all the code below outputs the same thing as if you not the if statement

$a="foobar"
$b=1

if $a==$b Then 
MsgBox(0,"","Matched")
else
MsgBox(0,"","Didnt Match")
endif

second of all how do you not understand what im asking... how are the string and number being compared?

most languages will not allow you to compare a string and number. I read in help file that a string will default to 0 in certain situations or take the first numbers if they are present.

Edited by dark_jedi

Share this post


Link to post
Share on other sites

which by the way when i run the script neither if statement gets executed which seems impossible.

You use wrong logic in your If Not statement.

Should be:

$a="foobar"
$b=1
if not ($a==$B) Then MsgBox(0,"no","foobar does not==1")
if $a==$b Then MsgBox(0,"yes","foobar==1")

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You use wrong logic in your If Not statement.

Where can I read which is the right logic for 'If Not'?

I searched in the help file and the forum, but I didn't find anything.

For this reason I write my scripts without 'If Not'. :D

Share this post


Link to post
Share on other sites

Where can I read which is the right logic for 'If Not'?

I searched in the help file and the forum, but I didn't find anything.

For this reason I write my scripts without 'If Not'. :D

I shown it in the post but let me explain a bit more:

When you take this statement:

if not 5=6 Then MsgBox(0,"test1","test1")

It will resolve "Not 5" first giving this statement:

if False=6 Then MsgBox(0,"test1","test1")

When you take this statement:

if not (5=6) Then MsgBox(0,"test2","Test2")

It will resolve "5=6 first giving this statement:

if not (False) Then MsgBox(0,"test2","Test2")

See the difference?


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Where can I read which is the right logic for 'If Not'?

I searched in the help file and the forum, but I didn't find anything.

For this reason I write my scripts without 'If Not'. :D

JdeB added braces to force $a==$b rather then If Not $a as "Operator precedence" takes place with Not being evaluated 1st without the braces used in the above code.

http://www.autoitscript.com/autoit3/files/...g_operators.htm

Edited by MHz

Share this post


Link to post
Share on other sites

@JdeB and MHz,

thanks for patience and explanations (I understood).

I know the section about operators in the help file. But I didn't find 'For using "If Not" you have to bracket the expression' or something like this there (maybe I'm the only). :D

Share this post


Link to post
Share on other sites

Briegel,

Review of operater precedence as you still seem unsure?

From highest precedence to lowest:

NOT

^

* /

+ -

&

< > <= >= = <> ==

AND OR

Not has highest precedence. Any value next to Not will be evaluted 1st. Not "If Not", just "Not" is the concern.

The examples below as quoted on the help page are quite good at explaining how to use braces to change precedence of how you want which expressions to be evalutated first.

First precedence of evaluation in bold

If Not 0 = 1 Then

as above Not 0 becomes 1.

Now to force a change of precedence, use braces.

If Not (0 = 1) Then

as above, 0=1 will be evaluted before Not does it's evaluation. A precedence change done with braces.

Hope alittle clearer. :D

Share this post


Link to post
Share on other sites

Thanks MHz,

you're very tirelessly. I really understood. But it is a difference between reading and understanding something. :D

I know I still have much to learn to code with AutoIt and ... english. :">

These are enough reasons for me to be a member in this forum.

again thanks

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  
Followers 0