Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Switch/case statement for Autohotkey


  • Please log in to reply
11 replies to this topic
helpme
  • Members
  • 41 posts
  • Last active: Oct 26 2015 06:41 AM
  • Joined: 22 Apr 2007
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.

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007

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:
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:
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.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
It's a planned feature for v2, which may take a while.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
nice pseudo case statement ManaUser, that looks almost like the real (not available) syntax. Your version also allows fallthrough, which is nice.

helpme
  • Members
  • 41 posts
  • Last active: Oct 26 2015 06:41 AM
  • Joined: 22 Apr 2007
Thanks for all your replies. Good to know it is coming out in ver2. Will be eagerly waiting.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

known as a kludge…

Here is another one, versions of which have been posted to the Forum already:
; . . .
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:
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
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.

fatribz
  • Guests
  • Last active:
  • Joined: --
its no kludge!
a switch statement compiles down to that

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Posted Image

drposts
  • Members
  • 4 posts
  • Last active: Oct 25 2015 04:55 PM
  • Joined: 12 Apr 2015

ManaUser:

 

Don't feel bad about your proposed LOOP CASE structure.  Not only is it easier to read, it executes faster.  That is why most programming languages include a DO CASE structure.  Hopefully AutoHotKey will offer us a DO CASE structure with the upcoming version 2 release.

 

Spoiler



drposts
  • Members
  • 4 posts
  • Last active: Oct 25 2015 04:55 PM
  • Joined: 12 Apr 2015

Unless you like SLOWWWW code don't use the function calls Switch() and SwitchX().  

 

They both run over 3 times slower than the in-line If statement implementations.  ManaUsers code did not fair as well with these trials.  I suspect that is due to the increased number of CASE statements and a failure to optimize CASE calls in the Autohotkey interpreter.

 

Spoiler



drposts
  • Members
  • 4 posts
  • Last active: Oct 25 2015 04:55 PM
  • Joined: 12 Apr 2015

Unless you like SLLOOOWWWW code don't use the Switch() and SwitchX() function calls.  They run over 3 times slower that the in-line IF statement implementations.

 

Spoiler



Peabianjay
  • Members
  • 12 posts
  • Last active: Nov 10 2015 05:46 PM
  • Joined: 23 Jun 2015

Here's another variation, which I kinda like since it circumvents the complication of duplicate labels (if you want to use more than one switch/case):

 

icaughta := "Tuna"

loop 1
{
    goto % select("Fish", icaughta)    
       Fish_Flounder:
           msgbox % "Flounder"
           break
       Fish_Tuna:
           msgbox % "Tuna!"
           break
       Fish_Sun:
           msgbox % "Sunfish."
           break
       Fish_NoMatch:
           msgbox % "NoMatch"
}
exitapp

select(case, var)
{
    if ( !islabel(case "_" var) )
         return % case "_NoMatch"
    else
         return % case "_" var
}