Very simple stuff, have no clue. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
GammaSerpentisFTW
Posts: 5
Joined: 17 Dec 2017, 09:50

Very simple stuff, have no clue.

17 Dec 2017, 10:05

In short:

Code: [Select all] [Download] GeSHi © Codebox Plus

if year > oldyear
msgbox yay

year = 2134
oldyear = 0
No msgbox, but if I change it to:

Got msgbox with "yay"

What am I missing here?
Full code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4575
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Very simple stuff, have no clue.  Topic is solved

17 Dec 2017, 10:30

It's always best to use parentheses. Regarding the results below, I didn't actually know the exact results until I tested. When you omit parentheses, it uses the legacy style syntax. Cheers.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


[EDIT:] Added 8 more MsgBox tests above.
Last edited by jeeswg on 17 Dec 2017, 11:50, edited 1 time in total.
GammaSerpentisFTW
Posts: 5
Joined: 17 Dec 2017, 09:50

Re: Very simple stuff, have no clue.

17 Dec 2017, 10:38

Thank you, jeeswg! It works, I will use parentheses from now on.
User avatar
jeeswg
Posts: 4575
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Very simple stuff, have no clue.

17 Dec 2017, 11:53

Btw when using functions or multiple parentheses, I believe the following lines in each pair are equivalent. I.e. there are some occasions when you can omit parentheses.

Code: [Select all] [Download] GeSHi © Codebox Plus

if InStr(vText, vNeedle)
if (InStr(vText, vNeedle))

if vPos := InStr(vText, vNeedle)
if (vPos := InStr(vText, vNeedle))

if (a = b) && (c = d)
if ((a = b) && (c = d))
Osprey
Posts: 293
Joined: 18 Nov 2017, 05:50

Re: Very simple stuff, have no clue.

17 Dec 2017, 14:07

You could've also solved it by adding % signs on either side of "oldyear":

Code: [Select all] [Download] GeSHi © Codebox Plus

if year > %oldyear%
msgbox yay

When using such legacy (non-parenthetical) syntax, you need the % signs around variables that are on the right side of assignments and comparisons. When you use parentheses, you don't need them, since parentheses tell AHK to use expression syntax. In expression syntax, everything is assumed to be a variable (or object) unless it has quotes around it, whereas, in legacy syntax, everything on the right of assignments and comparisons is considered to be an unquoted string unless there are % signs (i.e. in your original code, variable "year" was being compared to the 7-character string "oldyear", not the variable of the same name). In general, it's better to use the newer, parenthetical syntax, but you can't totally get away from legacy syntax, since many commands, like MsgBox, use it. It's one of the few annoying things about AHK.

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 85 guests