Trying to understand if statements Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Trying to understand if statements

Post by Flowgun » 27 Sep 2022, 08:17

Hello everyone,
so, I have two variables, a and b. Both contain images saved from the clipboard.
My script is working perfectly, but I had to go through trial and error to get the correct If statement to place, and I still don't understand it yet.
I can't find a good way to express this, so bare with me please.
In the documentation, it is stated that "if FoundColor != Blue" is equivalent to " if (FoundColor != "Blue")" . ( https://www.autohotkey.com/docs/commands/IfExpression.htm )
What I understood from the documentation is that if I use "If" in its function format (with the parentheses) instead of the "If" command (without parentheses), then not adding quotation marks to "Blue" would compare the value contained into the variable named "Blue" with the value of the variable "FoundColor".
Meaning if I want to compare the images stored in my variables a and b, I would write:
if (a != b)
But that doesn't work. I still have to enclose b with percentage signs for the script to work like this:
if (a != %b%)
and, (surprisingly?), this is the same as not including parentheses, like this:
if a != %b%

I thought I wouldn't need to use the percentage sign with if(expression). I still don't understand enough what's going on under the hood.
any explanation?

Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Re: Trying to understand if statements

Post by Flowgun » 27 Sep 2022, 08:25

Edit: Apparently I was mistaken
if a = %b%
is not equivalent to
if (a = %b%)
But I was not able to make that If statement work in the If(expression) format no matter what I tried. it only works like this:
if a = %b%
I'm going insane...

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Trying to understand if statements

Post by teadrinker » 27 Sep 2022, 08:42

Flowgun wrote: Both contain images saved from the clipboard.
You can only compare variables using = or == if they contain numbers or strings, not binary data. So, if you mean by images base64 or hex representation, you could compare such variables, if not, you cannot. Right way is if (a = b).


RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Trying to understand if statements

Post by RussF » 27 Sep 2022, 08:52

In legacy syntax, i.e.

Code: Select all

If a = b
a is considered the name of a variable and b is considered a literal, therefore if the contents of the variable a is equal to the literal string "b", then the expression is true.
The equivalent in expression form is

Code: Select all

IF (a = "b")
If you want to compare the contents of two variables named a and b, then the syntax in legacy would be

Code: Select all

If a = %b%
and in expression form would be

Code: Select all

If (a = b)
Does that help?

I always use expression form, not just because legacy is deprecated, but because it makes it easier for me (as a coder in other languages) to understand the syntax.

Russ

Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Re: Trying to understand if statements

Post by Flowgun » 27 Sep 2022, 09:03

RussF wrote:
27 Sep 2022, 08:52
In legacy syntax, i.e.

Code: Select all

If a = b
a is considered the name of a variable and b is considered a literal, therefore if the contents of the variable a is equal to the literal string "b", then the expression is true.
The equivalent in expression form is

Code: Select all

IF (a = "b")
If you want to compare the contents of two variables named a and b, then the syntax in legacy would be

Code: Select all

If a = %b%
and in expression form would be

Code: Select all

If (a = b)
Does that help?

I always use expression form, not just because legacy is deprecated, but because it makes it easier for me (as a coder in other languages) to understand the syntax.

Russ
This is exactly how I understand it, but in my case, it is not working! only the legacy format works...
This works great:

Code: Select all

if  a != %b%
This, no bueno:

Code: Select all

If (a = b)
This is what's been confusing me so much. maybe because the content of variables is an image? (I don't understand yet how they are stored internally). Only the legacy format works in this particular case. Authotkey version Version 1.1.34.04.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to understand if statements  Topic is solved

Post by Rohwedder » 27 Sep 2022, 09:05

Hallo,
Variables to which ClipboardAll has been assigned must be compared to each other (but not directly to ClipboardAll) by means of If[Not]Equal, If Var1 = %Var2%, or If Var1 != %Var2%
https://www.autohotkey.com/docs/misc/Clipboard.htm#Notes
Only the legacy format works in this particular case!

Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Re: Trying to understand if statements

Post by Flowgun » 27 Sep 2022, 09:16

Rohwedder wrote:
27 Sep 2022, 09:05
Hallo,
Variables to which ClipboardAll has been assigned must be compared to each other (but not directly to ClipboardAll) by means of If[Not]Equal, If Var1 = %Var2%, or If Var1 != %Var2%
https://www.autohotkey.com/docs/misc/Clipboard.htm#Notes
Only the legacy format works in this particular case!
Thank you! This was it :D . I didn't know that binary data like the one extracted from ClipboardAll has particular limitations. It is stated in the link you shared " if ClipSaved1 != %ClipSaved2% ; This must be an old-style IF statement, not an expression."

I was trying to update some old code to newer syntax, and I simply couldn't figure out a way to make these If Statements work with the If(expression) format. It made me feel like a noob and I started questioning all of my knowledge and understanding about scripting.

RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Trying to understand if statements

Post by RussF » 27 Sep 2022, 09:17

I'm educated once again - thanks @Rohwedder!

Russ

Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Re: Trying to understand if statements

Post by Flowgun » 27 Sep 2022, 12:44

That brings another question. I haven't dabbled with AHK v2 yet, but it seems to remove legacy if. Would the if(expression) work for binary code in V2 while it doesn't work for V1?

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to understand if statements

Post by Rohwedder » 28 Sep 2022, 03:50

No!-No!:

Code: Select all

#Requires AutoHotkey v2.0-beta.10-
C1 := ClipboardAll()
C2 := ClipboardAll()
MsgBox C1=C2?"Yes":"No!"
MsgBox ClipboardAll()=ClipboardAll()?"Yes":"No!"

Post Reply

Return to “Ask for Help (v1)”