ternary operator Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

ternary operator

20 Oct 2017, 09:02

i would like to shorten code similar to following one:

Code: Select all

        if choice = left
          f__move("left")

        if choice = right
          f__move("right")

        if choice = up
          f__move("up")

        if choice = down
          f__move("down")
because AHK does not have a possibility to have 2 commands on one line, i'm thinking about using ternary operator to run a function
User avatar
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

Re: ternary operator

20 Oct 2017, 09:18

A ternary operator is logically equivalent to an if/else block, and you do not have one of those.
I am suspecting though that you want one, as choice can only be one thing.

It would be very complicated though, you would need 3 nested ternarys
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

Re: ternary operator

20 Oct 2017, 09:23

thank you for quick reply, i dont need to have it in one line
can you please show to me how shorten the code?
my example was bad, in some cases i want to run different function
User avatar
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

Re: ternary operator

20 Oct 2017, 09:26

I just did.

f__move(choice) is basically equivalent to:

Code: Select all

        if choice = left
          f__move("left")
        else if choice = right
          f__move("right")
        else if choice = up
          f__move("up")
        else if choice = down
          f__move("down")
Also, if you say "I don't need it on one line", then why ask for a ternary, as that is exactly the point of a ternary, to collapse an if/else block into one line?

Also, it is not suitable to do with a ternary, as ternarys are for assignment, and you want to execute a function, not assign
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

Re: ternary operator

20 Oct 2017, 09:33

my point is TO SHORTEN THE CODE
i understood, thank you.
my example was bad, in some cases i want to run different function

Code: Select all

        if choice = left
          f__move("left")
        else if choice = right
          f__different_function()
would like to have something like

Code: Select all

if choice = left         f__move("left")
if choice = right        f__different_function()
(else command is not necessary here)
User avatar
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

Re: ternary operator

20 Oct 2017, 09:36

Terka wrote:my point is TO SHORTEN THE CODE
would like to have something like

Code: Select all

if choice = left         f__move("left")
if choice = right        f__different_function()
(else command is not necessary here)
Is it possible for choice to hold both left AND right?
No.
So whilst you do not NEED an else, it is more CPU efficient.
Last edited by evilC on 20 Oct 2017, 09:41, edited 1 time in total.
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

Re: ternary operator

20 Oct 2017, 09:53

evilC, thank you!
User avatar
jeeswg
Posts: 5146
Joined: 19 Dec 2016, 01:58
Location: UK

Re: ternary operator

20 Oct 2017, 12:32

This is generally quite unusual code, and might not work as expected.

Code: Select all

if (choice = "left" || choice = "right") f__move(choice)
if (choice = "up" || choice = "down") f__different_function()
E.g. the w hotkey triggers f__move and the line underneath the 'if' line.
E.g. instead, the q hotkey has various alternatives.

Code: Select all

q::
choice := "left"
;choice := ""
if (choice = "left" || choice = "right"
|| choice = "up" || choice = "down")
	f__move(choice)
if (choice ~= "^(left|right|up|down)$") ;preferred
	f__move(choice)
if RegExMatch(choice, "^(left|right|up|down)$")
	f__move(choice)
if choice in left,right,up,down
	f__move(choice)
return

w::
choice := "left"
;choice := ""
if (choice = "left" || choice = "right") f__move(choice)
	MsgBox, % "_"
return

f__move(x)
{
	MsgBox
}
Helgef
Posts: 3192
Joined: 17 Jul 2016, 01:02
Contact:

Re: ternary operator

20 Oct 2017, 12:52

Hello :wave:
Another one,

Code: Select all

(choice = "left" || choice = "right") ? f__move(choice) : (choice = "up" || choice = "down") ? f__different_function(choice) : f__else(choice)
Eg,

Code: Select all

for k, choice in ["left","right","up","down","out"]
	(choice = "left" || choice = "right") ? f__move(choice) : (choice = "up" || choice = "down") ? f__different_function(choice) : f__else(choice)

f__move(a){
	msgbox % A_ThisFunc "`n" a
}
f__different_function(a){
	msgbox % A_ThisFunc "`n" a
}
f__else(a){
	msgbox % A_ThisFunc  "`n" a
}
Cheers
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

Re: ternary operator

23 Oct 2017, 01:37

thank you all!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], CyL0N, Google [Bot], Hampoon and 58 guests