| View previous topic :: View next topic |
| Author |
Message |
helpme
Joined: 22 Apr 2007 Posts: 33
|
Posted: Thu Jun 07, 2007 6:39 am Post subject: Switch/case statement for Autohotkey |
|
|
Dear Autohotkey experts,
Is there a switch/case statement equivalent for Autohotkey?
I would like to make my code more readable, if-then-else statements are not very readable when too long. |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 1121
|
Posted: Thu Jun 07, 2007 7:53 am Post subject: Re: Switch/case statement for Autohotkey |
|
|
| helpme wrote: | Dear Autohotkey experts,
Is there a switch/case statement equivalent for Autohotkey?
I would like to make my code more readable, if-then-else statements are not very readable when too long. |
I don't think so, but you can write your if-else code like this to improve readability:
| Code: | if (MyVar = "A")
{
Send A
SoundBeep
}
else if (MyVar = "B")
{
Send B
SoundBeep
}
else if (MyVar = "C")
{
Send C
SoundBeep
}
else
{
Send X
SoundBeep
} |
[Edit] I did have one other idea... I think this is what's known as a kludge:
| Code: | If IsLabel("Case-" . MyVar)
Loop 1 {
Goto Case-%MyVar%
Case-A:
Send A
SoundBeep
Break
Case-B:
Send B
SoundBeep
Break
Case-C:
Send C
SoundBeep
Break
} Else {
Send X
SoundBeep
} |
Heh, that's probably worse than the first one, though it does look more like a real case statment. |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Thu Jun 07, 2007 9:28 am Post subject: |
|
|
It's a planned feature for v2, which may take a while. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Thu Jun 07, 2007 2:37 pm Post subject: |
|
|
nice pseudo case statement ManaUser, that looks almost like the real (not available) syntax. Your version also allows fallthrough, which is nice. _________________
(Common Answers) |
|
| Back to top |
|
 |
helpme
Joined: 22 Apr 2007 Posts: 33
|
Posted: Fri Jun 08, 2007 12:59 am Post subject: |
|
|
| Thanks for all your replies. Good to know it is coming out in ver2. Will be eagerly waiting. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Fri Jun 08, 2007 8:11 pm Post subject: |
|
|
| ManaUser wrote: | | known as a kludge… | Here is another one, versions of which have been posted to the Forum already: | Code: | ; . . .
Switch(12)
Switch(Case) {
GoTo % IsLabel("Case-" Case) ? "Case-" Case : "Case-Default"
Case-A:
MsgBox Case-A
Return
Case-12:
MsgBox Case-12
Return
Case-Default:
MsgBox Default
Return
}
; . . .
MsgBox Next Statements | Here a function definition imitates a block. This one has two extra code lines, not necessary with true Switch/Case commands, but the problem with these workarounds is that you have difficulties if you need more of them in one script. The case labels are now true labels, which have to be unique in the whole script, that is, you need to keep track of unused name variants: | Code: | SwitchX(11)
SwitchX(Case) {
GoTo % IsLabel("CaseX-" Case) ? "CaseX-" Case : "CaseX-Default"
CaseX-A:
MsgBox Case-A
Return
CaseX-12:
MsgBox Case-12
Return
CaseX-Default:
MsgBox Default
Return
} |
Another important case is selecting one of several values (expression-switch). Here is a workaround for that | Code: | select(i,x1,x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") {
Return x%i%
} | You might need another function called in the first parameter, which converts other kind of data to numbers. The drawback comes from the lack of lazy parameter evaluation in AHK: all the parameters are assigned to local variables, although only one is used, which is slow. |
|
| Back to top |
|
 |
fatribz Guest
|
Posted: Thu Dec 15, 2011 2:06 am Post subject: no kludge! |
|
|
its no kludge!
a switch statement compiles down to that |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
|