Math syntax parsing issue Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bonobo
Posts: 81
Joined: 03 Sep 2023, 20:13

Re: Math syntax parsing issue

Post by bonobo » 11 Sep 2023, 07:46

lexikos wrote:
10 Sep 2023, 06:44

In the current implementation (and this should not be taken as any kind of guarantee), a variable is not dereferenced before becoming input to an operator or function. The variable itself is the input, and the operator or function dereferences the variable if that is what it is designed to do. An operator will use whatever value the variable has when the operator is evaluated, not the value one operand had before the other operand was evaluated. This is consistent across all operators, so for instance, &++x takes a variable reference, (++x) /= 2 increments and then halves, and ++x + ++x does what it does.
This helps a lot, thanks.

It occurs to me that if, for whatever reason, you really want the values (rather than variable reference) be used, it's possible to use throwaway variable assignments:

For example, compare this

Code: Select all

	i := 1
	j := ++i + ++i ; 6
with this

Code: Select all

	i := 1
	j := (_x := ++i) + (_y := ++i) ; 5
Also compare this:

Code: Select all

	i := 1
	arr2 := [++i, ++i, ++i] ; [4,4,4]
with this:

Code: Select all

	i := 1
	arr2 := [_x := ++i, _y := ++i, _z := ++i] ; [2,3,4]

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 11 Sep 2023, 08:10

bonobo wrote:
11 Sep 2023, 07:46
Nice comparison illustration of this behavior... thanks

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Math syntax parsing issue

Post by lexikos » 11 Sep 2023, 22:56

Number(++i) would avoid throwaway variables, as would (++i + 0).

User avatar
andymbody
Posts: 1034
Joined: 02 Jul 2017, 23:47

Re: Math syntax parsing issue

Post by andymbody » 12 Sep 2023, 00:14

lexikos wrote:
11 Sep 2023, 22:56
Number(++i) would avoid throwaway variables, as would (++i + 0).
I will use ++1+0 as the solution. It works! :thumbup:
Thank you!

User avatar
TheArkive
Posts: 1032
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Math syntax parsing issue

Post by TheArkive » 12 Sep 2023, 00:30

This seems very reminiscent of AHK v1, the whole +0 thing. Oh the memories :D

Post Reply

Return to “Ask for Help (v2)”