Trying to understand if statements Topic is solved
Trying to understand if statements
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?
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?
Re: Trying to understand if statements
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...
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...
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Trying to understand if statements
In legacy syntax, i.e.
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
If you want to compare the contents of two variables named a and b, then the syntax in legacy would be
and in expression form would be
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
Code: Select all
If a = b
The equivalent in expression form is
Code: Select all
IF (a = "b")
Code: Select all
If a = %b%
Code: Select all
If (a = b)
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
Re: Trying to understand if statements
This is exactly how I understand it, but in my case, it is not working! only the legacy format works...RussF wrote: ↑27 Sep 2022, 08:52In legacy syntax, i.e.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.Code: Select all
If a = b
The equivalent in expression form isIf you want to compare the contents of two variables named a and b, then the syntax in legacy would beCode: Select all
IF (a = "b")
and in expression form would beCode: Select all
If a = %b%
Does that help?Code: Select all
If (a = b)
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 works great:
Code: Select all
if a != %b%
Code: Select all
If (a = b)
Re: Trying to understand if statements Topic is solved
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!
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!
Re: Trying to understand if statements
Thank you! This was it . 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."Rohwedder wrote: ↑27 Sep 2022, 09:05Hallo,
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!
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.
Re: Trying to understand if statements
I'm educated once again - thanks @Rohwedder!
Russ
Russ
Re: Trying to understand if statements
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?
Re: Trying to understand if statements
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!"