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

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

Code: Select all

if year > 0
	msgbox yay
Got msgbox with "yay"

What am I missing here?
Full code:

Code: Select all

string = "year: 2134 month: 11"
oldyear := 0
string := SubStr(string, InStr(string, "year: ") + 6) ; =2134 month: 11"

year := SubStr(string, 1, 1)
j := 2
Loop
	{
	If Not (SubStr(string, j, 1) = " ")
		{
		year := year . SubStr(string, j, 1)
		j++
		}
	Else Break
	}
if year > oldyear
	msgbox yay
User avatar
jeeswg
Posts: 5152
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

q:: ;test a = b and (a = b)
var1 := 10
var2 := 10

if 10 = var2
	MsgBox, 1
if 10 = %var2%
	MsgBox, 2
if (10 = var2)
	MsgBox, 3 ;works (recommended)
if (10 = %var2%)
	MsgBox, 4

if var1 = var2
	MsgBox, 5
if var1 = %var2%
	MsgBox, 6 ;works (legacy style)
if (var1 = var2)
	MsgBox, 7 ;works (recommended)
if (var1 = %var2%)
	MsgBox, 8

if 10 = var2
	MsgBox, 9
if 10 = %var2%
	MsgBox, 10
if (10 = var2)
	MsgBox, 11 ;works
if (10 = %var2%)
	MsgBox, 12

if 10 = 10
	MsgBox, 13
if 10 = %10%
	MsgBox, 14 ;'works' (but it is not comparing 10 with 10: it is getting the contents of a variable called 10 in each example, the variable '10' is not defined, so it is comparing a blank string with a blank string)
if (10 = 10)
	MsgBox, 15 ;works
if (10 = %10%)
	MsgBox, 16
return
[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: 5152
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

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: 294
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

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: Google [Bot], Player, pneumatic, roysubs and 37 guests