what does "=" do?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

what does "=" do?

01 Jun 2018, 13:33

From what I understand, the equal sign is used to assign. For example, in the following

a = now is the time

a is assigned the value "now is the time" (a string, without, of course, the quotes).

How does one make an arithmetic comparison then?

At the documentation for OPERATORS, =, while it is shown, IS NOT DEFINED. There the doc states:
Equal (=), case-sensitive-equal (==), and not-equal (<> or !=). The operators != and <> are identical in function. The == operator behaves identically to = except when either of the inputs is not a number, in which case == is always case sensitive and = is always case insensitive (the method of insensitivity depends on StringCaseSense). By contrast, <> and != obey StringCaseSense.
This does not say what = does.

The following sets VAR equal to 3

x := 1
y := 3
var := x > y ? 2 : 3

Why does the following not set VAR equal to 3?

x := 1
y := 3
var := x = y ? 2 : 3

And, how do I make that last example work?
Last edited by joefiesta on 01 Jun 2018, 14:07, edited 1 time in total.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: what does "=" do?

01 Jun 2018, 13:53

joefiesta wrote:
Why does the following not set VAR equal to 2?

x := 1
y := 3
var := x = y ? 2 : 3

And, how do I make that last example work?
It doesn't set VAR to 2 because x and y are not equal. Hence, the else branch, which begins with :, of the ternary ("a shorthand replacement for the if-else statement") gets executed: Var is 3

Code: Select all

x := 3
y := 3
var := x = y ? 2 : 3  	; true -> Var := 2
msgbox % var

x := 1
y := 3
var := x = y ? 2 : 3 	; false -> Var := 3
msgbox % var
It works for me...

Of course, = can also be used for assigning ("traditional syntax"), but this will be removed in AHK v2, since this assigning method from the ancient days of early AHK often leads to confusion. Then, = will exclusively be used for comparisons like above... afaik.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: what does "=" do?

01 Jun 2018, 14:10

Ok, now you edited your original post...

Code: Select all

Why does the following not set VAR equal to 3?

x := 1
y := 3
var := x = y ? 2 : 3
But it does set VAR to 3...
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: what does "=" do?

01 Jun 2018, 14:12

Gregster.... sorry, I just realized my error... The question should have been, and is now(just edited it), "why does the 2nd example not set var equal to 3?"

Secondly, I should have supplied the code that was problematic. It is:

Code: Select all

resize := 0
pos_w := 555
pos_tempW := 666
new_W := resize = 1 ? %pos_W% : %pos_tempW%
msgbox % new_w               
I realize now that the percent signs in the ternary statement should not exist.

thanks for your reply.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: what does "=" do?

01 Jun 2018, 14:28

No problem.
I realize now that the percent signs in the ternary statement should not exist.
Yes, exactly, because you are already in expression mode.

I think what you did is called a "double deref". AHK will then look for a variable that has the name of the contents of this variable. Which will bring up blanks in this case, I suppose.

But I think even after the edit, there is still something going wrong in your initial post.

Code: Select all

Why does the following not set VAR equal to 3?

x := 1
y := 3
var := x = y ? 2 : 3
It actually sets var to 3
Last edited by gregster on 01 Jun 2018, 14:33, edited 1 time in total.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: what does "=" do?

01 Jun 2018, 14:32

I've been writing this language for some 10 years, and I still can't always figure out the stupid expression crap. I think V2 fixes that--but from what I've seen, V2 is so drastically different, it will take another 10 years before I get there. Gee, I'm 67. Hope I make it.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: what does "=" do?

01 Jun 2018, 14:34

Yeah, I understand. I had a different, but similar case today, which took me some time to figure out.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 213 guests