Page 1 of 1
ternary operator
Posted: 20 Oct 2017, 09:02
by Terka
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
Re: ternary operator
Posted: 20 Oct 2017, 09:18
by evilC
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
Re: ternary operator
Posted: 20 Oct 2017, 09:19
by evilC
Besides, you can do the whole lot on one line anyway:
f__move(choice)
Re: ternary operator
Posted: 20 Oct 2017, 09:23
by Terka
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
Re: ternary operator
Posted: 20 Oct 2017, 09:26
by evilC
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
Re: ternary operator
Posted: 20 Oct 2017, 09:33
by Terka
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)
Re: ternary operator
Posted: 20 Oct 2017, 09:36
by evilC
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.
Re: ternary operator
Posted: 20 Oct 2017, 09:38
by evilC
Ah, correction, you may need to use:
if (choice = "left") f__move("left") (Add brackets and quotes around left)
Re: ternary operator Topic is solved
Posted: 20 Oct 2017, 09:40
by evilC
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()
Re: ternary operator
Posted: 20 Oct 2017, 09:53
by Terka
evilC, thank you!
Re: ternary operator
Posted: 20 Oct 2017, 12:32
by jeeswg
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
}
Re: ternary operator
Posted: 20 Oct 2017, 12:52
by Helgef
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
Re: ternary operator
Posted: 23 Oct 2017, 01:37
by Terka
thank you all!