ternary operator Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Terka
Posts: 113
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] [Download] GeSHi © Codebox Plus

        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: 3546
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: 113
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: 3546
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] [Download] GeSHi © Codebox Plus

        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: 113
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] [Download] GeSHi © Codebox Plus

        if choice = left
f__move("left")
else if choice = right
f__different_function()

would like to have something like

Code: [Select all] [Download] GeSHi © Codebox Plus

if choice = left         f__move("left")
if choice = right f__different_function()

(else command is not necessary here)
User avatar
evilC
Posts: 3546
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] [Download] GeSHi © Codebox Plus

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

Re: ternary operator

20 Oct 2017, 09:38

Ah, correction, you may need to use:

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

Re: ternary operator  Topic is solved

20 Oct 2017, 09:40

You could also do like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

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

Re: ternary operator

20 Oct 2017, 09:53

evilC, thank you!
User avatar
jeeswg
Posts: 2637
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] [Download] GeSHi © Codebox Plus

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] [Expand] [Download] GeSHi © Codebox Plus

Helgef
Posts: 2391
Joined: 17 Jul 2016, 01:02
Contact:

Re: ternary operator

20 Oct 2017, 12:52

Hello :wave:
Another one,

Code: [Select all] [Download] GeSHi © Codebox Plus

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

Eg,

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Cheers
Terka
Posts: 113
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], HotKeyIt, kczx3 and 45 guests