## 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

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
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator

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
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator

Besides, you can do the whole lot on one line anyway:

`f__move(choice)`
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

### Re: ternary operator

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
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator

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

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)
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator

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.
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator

Ah, correction, you may need to use:

`if (choice = "left") f__move("left")` (Add brackets and quotes around left)
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

### Re: ternary operator  Topic is solved

You could also do like this:

Code: Select all

``````if (choice = "left" || choice = "right") f__move(choice)
if (choice = "up" || choice = "down") f__different_function()
``````
Terka
Posts: 127
Joined: 05 Nov 2015, 04:59

### Re: ternary operator

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

### Re: ternary operator

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

Hello
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

thank you all!

### Who is online

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