AutoHotkey Community

It is currently May 25th, 2012, 7:37 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: June 7th, 2007, 7:39 am 
Offline

Joined: April 22nd, 2007, 11:52 am
Posts: 33
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 7th, 2007, 8:53 am 
Offline

Joined: May 24th, 2007, 3:45 am
Posts: 1121
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2007, 10:28 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
It's a planned feature for v2, which may take a while.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2007, 3:37 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
nice pseudo case statement ManaUser, that looks almost like the real (not available) syntax. Your version also allows fallthrough, which is nice.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2007, 1:59 am 
Offline

Joined: April 22nd, 2007, 11:52 am
Posts: 33
Thanks for all your replies. Good to know it is coming out in ver2. Will be eagerly waiting.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2007, 9:11 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: no kludge!
PostPosted: December 15th, 2011, 3:06 am 
its no kludge!
a switch statement compiles down to that


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 15th, 2011, 4:19 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5461
Location: the tunnel(?=light)
Image

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], gemisigo, HotKeyIt, kwfine, lblb, RoAltmann and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group