Can GoTo's be used with Ternary Operators?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NiptheZephyr
Posts: 42
Joined: 01 Aug 2020, 21:33

Can GoTo's be used with Ternary Operators?

Post by NiptheZephyr » 06 Jul 2022, 01:41

...if not, why not?

Howdy all,

I'm looking for the most succinct way to code the following situation. I want to check a block of text to see if it contains either string A, string B or neither, and based upon the result go down one of two different paths, or just continue down the main path if nothing was true. I can accomplish this with the following:

Code: Select all

testlabel:
MsgBox, Case 1 path.
Return

testlabel2:
MsgBox, Case 2 path.
Return

testlabel3:
MsgBox, Continue main path
Return

+<::
	If (clipboard ~= "case one")
		GoTo testlabel
	Else If (clipboard ~= "case 2")
		GoTo testlabel2
MsgBox, Neither happened
Return
I tried to use a ternary operator style to do the above, which doesn't seem to work and I don't know why:

Code: Select all

testlabel:
MsgBox, Case 1 path.
Return

testlabel2:
MsgBox, Case 2 path.
Return

testlabel3:
MsgBox, Continue main path
Return

clipboard ~= "case 1" ? GoTo testlabel : clipboard ~= "case 2" ? GoTo testlabel2 : GoTo testlabel3
My understanding of Ternary was that ? meant "check if the preceeding statement is true". If true then do what is left of : if false do what is right of :. What about this case makes it not work?

Rohwedder
Posts: 7558
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Can GoTo's be used with Ternary Operators?

Post by Rohwedder » 06 Jul 2022, 02:27

Hallo,
not Goto itself, but its label parameter.
Try:

Code: Select all

clipboard = case 2

Goto,% clipboard ~= "case 1" ? "testlabel": clipboard ~= "case 2" ? "testlabel2" : "testlabel3"
; the parameter of Goto was made into an expression with %

testlabel:
MsgBox, Case 1 path.
Return

testlabel2:
MsgBox, Case 2 path.
Return

testlabel3:
MsgBox, Continue main path
Return
If true then do what is left of : if false do what is right of :
"do" works only for expressions, not for commands.

Code: Select all

True ? q := 1 + 1 ;works
MsgBox, works 1 + 1 = %q%
True ? SoundBeep ;works not, no Beep! SoundBeep is a command
MsgBox, works not, no Beep!
SoundBeep, 1000, True ? 1000 : 0 ;parameters of SoundBeep can be expressions
MsgBox, works, 1 Second Beep

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

Re: Can GoTo's be used with Ternary Operators?

Post by lexikos » 06 Jul 2022, 04:14

NiptheZephyr wrote:
06 Jul 2022, 01:41
My understanding of Ternary was that ? meant "check if the preceeding statement is true". If true then do what is left of : if false do what is right of :. What about this case makes it not work?
Ternary is not an if statement; it is an operator for use within an expression. Each part of an expression is designed to yield a value (evaluate to a value) for use by the next part of the expression. Goto is a control flow statement (and is also known as a command), not a function or operator; it transfers control to some other line, so there is no concept of evaluating it. Something like if (a < b + goto end) makes no sense.

I would highly recommend learning not to rely on goto at all. For your case, if a normal if-else statement with blocks isn't sufficient, divide your code into functions with specific purposes, and call them instead of jumping with goto.

Post Reply

Return to “Ask for Help (v1)”