AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Switch/case statement for Autohotkey

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
helpme



Joined: 22 Apr 2007
Posts: 33

PostPosted: Thu Jun 07, 2007 6:39 am    Post subject: Switch/case statement for Autohotkey Reply with quote

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
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 1121

PostPosted: Thu Jun 07, 2007 7:53 am    Post subject: Re: Switch/case statement for Autohotkey Reply with quote

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
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Thu Jun 07, 2007 9:28 am    Post subject: Reply with quote

It's a planned feature for v2, which may take a while.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Thu Jun 07, 2007 2:37 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
helpme



Joined: 22 Apr 2007
Posts: 33

PostPosted: Fri Jun 08, 2007 12:59 am    Post subject: Reply with quote

Thanks for all your replies. Good to know it is coming out in ver2. Will be eagerly waiting.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Jun 08, 2007 8:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
fatribz
Guest





PostPosted: Thu Dec 15, 2011 2:06 am    Post subject: no kludge! Reply with quote

its no kludge!
a switch statement compiles down to that
Back to top
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Thu Dec 15, 2011 3:19 am    Post subject: Reply with quote


_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group