 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Tue Mar 01, 2005 4:54 am Post subject: What's the proper use of () and "" in If statement |
|
|
Can anyone please explain the proper use of () and "" in If statements?
More important perhaps is to understand the logic behind it all so I can get it right the first time.
I am forever using the wrong syntax at the wrong time, making for hard to track down bugs.
I can't find where in the help file it addresses this specifically.
Here's an example:
| Code: | a=yes
b=2
if (a = "yes") ;this works
msgbox 1
if (a = yes) ;this does not work
msgbox 2
if a = yes ;this works
msgbox 3
if a = "yes" ;this does not work
msgbox 4
if (a = "yes" and b = 2) ;this works
msgbox 5
if (a = yes and b = 2) ;this does not work
msgbox 6
if a = yes and b = 2 ;this does not work
msgbox 7
|
Thanks _________________ ChrisM |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Tue Mar 01, 2005 5:42 am Post subject: |
|
|
Ok, first I have to explain what interpolation is. It's how AutoHotkey works with variables in your code. In it's most basic form, interpolation is simply substituting something for the data it represents. Usually, you tell AutoHotkey to interpolate variables by enclosing their names in percentage signs, like this:
AutoHotkey sees the percentage signs and knows to substitute the contents of the "ms" variable in for the reference to it. That's what interpolation is. Now that that's clear, I'll explain how it works in expressions.
As you probably know, the parentheses tell AutoHotkey that you're giving it an expression. Once it knows it's inside an expression, it switches to a different method of interpolation. It will take any plain text it sees, percentage signs or not, and look for a variable with that name. If it exists, it will substitute the contents. If it doesn't, it will simply make that area blank. Of course, this convenient interpolation method presents a problem for inputting plain text. That's where the quotes come in. When AutoHotkey sees the quotes, it knows to halt the interpolation and take the text at face value.
Now, I'll explain why each of the examples you posted will or will not work, based on the explanation above.
This works as intended; AutoHotkey sees that it's in an expression, so it interpolates the plain text "a" and compares it to the string "yes".
This is a valid expression, but it will only be true if the "a" variable contains the same data as the "yes" variable, since all plain text is interpolated. If "yes" is nonexistent, it will only be true if "a" is blank or also nonexistent.
This works, because it isn't an expression. In a regular if statement, the string on the left side of the statement is interpolated, but the string on the right side is not. No extra punctuation is necessary for AutoHotkey to know what to do.
This is, again, a valid if statement, but it will only be true if the "a" variable contains the string "yes" (quotes included). In an expression, AutoHotkey knows that the quotes are only there to tell it to halt interpolation, and it discards them. In an if statement, it takes them literally.
| Code: | | if (a = "yes" and b = 2) |
This works because the interpolation rule does not apply to numbers (nor does it apply to operators like and, or, and not).
| Code: | | if (a = yes and b = 2) |
This won't work; see above.
| Code: | | if a = yes and b = 2 |
This won't work as intended because AutoHotkey doesn't know you want it to be an expression. It thinks you want to check whether the "a" variable contains the string "yes and b = 2". That's what the parantheses are for, to inform AutoHotkey of what it is.
Well, I had fun making that. I'm sure Chris already has a nice, detailed explanation somewhere on the site, but I'm not in a very doc-y mood tonight. |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Tue Mar 01, 2005 6:41 am Post subject: |
|
|
Thanks for that jonny. Well thought out!
It certainly helps me understand it all better.
But it doesn't seem very intuitive does it.
I mean so many ways to reference and use a variable.
Especially:
| Quote: | if (a = yes)
... If "yes" is nonexistent, it will only be true if "a" is blank or also nonexistent. |
I would think if "yes" is nonexistent it should be false no matter what's in "a".
I also think AutoHotkey should give an error if "a" is nonexistent.
While I'm at it ... those percentage signs ... sometimes you need them, sometimes
you dont ... that's tripped me up way too often also. I know it's more coding
but it would seem best to always require them whenever a variable is being read.
Stricter syntax = less bugs.
I know every language has its 'features', and it's the way it is for convienince
and whatever, but maybe the syntax rules are just a little too easygoing.
I'm not complaining, but ... um ... well ... ok ... I am complaining ... sorry 'bout that _________________ ChrisM |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Tue Mar 01, 2005 7:00 am Post subject: |
|
|
"Non-existent" doesn't constitute an error, it simply means the variable has been referenced before it's been created. Since [thankfully] there are no variable declarations in AutoHotkey, this isn't punishable by law. Of course, in some cases, like this one, it's a logical error, meaning the program will compile and run fine, but won't work as the programmer intended because of a logical flaw in the operation. Logical errors are much harder to spot than syntax errors, obviously. |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Tue Mar 01, 2005 7:44 am Post subject: |
|
|
Thx, ChrisM for asking this. Now I know me, myself and I weren't the only ones who needs this to be clearified.
Thx, Jonny for explaining it. I'm quite impressed. That's the way I think a HowTo should be created.
 |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 01, 2005 8:15 am Post subject: |
|
|
| jonny wrote: | | As you probably know, the parentheses tell AutoHotkey that you're giving it an expression. Once it knows it's inside an expression, it switches to a different method of interpolation. |
I think it was a bad decision to introduce a different expression syntax. What's wrong with if (%a% = yes)? Two non-uniform syntaxes for expressing the same thing is confusing. |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Tue Mar 01, 2005 8:44 am Post subject: |
|
|
| Quote: | | Two non-uniform syntaxes for expressing the same thing is confusing. | For me its that simple - what's enclosed in brackets and should be interpreted as a literal string has to be referenced with enclosing "". Otherwise its seen as a variable*.
* cause the interpolation rule does not apply to numbers (nor does it apply to operators like and, or, and not |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Tue Mar 01, 2005 1:19 pm Post subject: |
|
|
| Guest wrote: | | I think it was a bad decision to introduce a different expression syntax. What's wrong with if (%a% = yes)? Two non-uniform syntaxes for expressing the same thing is confusing. | One reason it was done this way is to support arrays in expressions:
Var := Array%i%
Another reason is the planned support for calling functions, which would look messy with percent signs:
Var := %MyFunction(Param1, Param2)%
Also, expressions (especially long ones) seem easier to read without percent signs around each variable. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|