Jump to content

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

Case/Switch


  • Please log in to reply
44 replies to this topic
Demokos
  • Members
  • 105 posts
  • Last active: Jan 20 2011 06:28 PM
  • Joined: 28 Dec 2005
You can look at a recent topic here, for example...

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
[quote name="Chris Posted]I think you're right that this is important. I'll try to get it done within the next 30 days.
[/quote]or not
looks like a very long 30 days
Never lose.
WIN or LEARN.

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006

I'm a little confused on how to go about using these Switch-Case scenarios? I want to use a switch case in my code, how do I do this? Thanks.


Here's a very simple illustration of a switch/case structure:
Switch ( A_HH // 6 )
{
	Case 0:
		msgbox, Good Night
	Case 1:
		msgbox, Good Morning
	Case 2:
		msgbox, Good Afternoon
	Case 3:
		msgbox, Good Evening
	Default:
		msgbox, Good Day
}
Obviously, it isn't valid AHKscript.

Here's the if/else equivalent in AHKscript
SwitchVar := ( A_Now // 6 )
if (SwitchVar = 0)
	msgbox, Good Night
else if (SwitchVar = 1)
	msgbox, Good Morning
else if (SwitchVar = 2)
	msgbox, Good Afternoon
else if (SwitchVar = 3)
	msgbox, Good Evening
else 
	msgbox, Good Day

Switch/case is a cosmetic feature, though it could be implemented/optimized to be faster than if/else.

Scratch
  • Members
  • 108 posts
  • Last active: Mar 16 2014 08:02 PM
  • Joined: 22 Jan 2009
Before AHK, i was into spreadsheet macro's and functions and made heavily use of Excel CHOOSE() function,

For use in AHK, it could look something like this:

Choose Greet, msgbox Good , 0;Night, 1;Morning, 2;Afternoon, ERR;Day


Very short, yet readable,

Readabillity would suffer if the parameters would be a series of complex nested functions themselves but in that case { blocks of IF ELSE or CASE statements look more appropiate

But I think its a nice straightforward function to have and i bet it would be compiled into faster code using less memory than a block of multiple IF & MSGBOX statements


Come to think of it, we have hotkeys, hotstrings, how about HOTVARS?

A Hotvar could for example look like this and execute as semi-asynchronous thread
whenever the value of the watched variable is changed in memory

::greet=0
Msgbox Good Morning
Gosub MakeCoffe
return

::greet=1
MsgBox Good Evening
Gosub CloseCurtains
return

::Harddiskspace<100
MsgBox Less than 100 MB Left, free up some space...
return

It would be like semaphore triggers

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I think that Excel macros are more data-centric than AutoHotkey scripts; "Choose" would make less sense in AutoHotkey. You can get something mostly equivalent using a pseudo-array:
Choice0=Night
Choice1=Morning
Choice2=Afternoon
if Choice%Greet% !=
    Choice := Choice%Greet%
else
    Choice = Day
It would also be possible to write Choose() as a script function; usage could be something like Choose(Greet, 0, "Night", 1, "Morning", 2, "Afternoon", "Day"). Every second parameter would be the value to match the first parameter (Greet) to. If there are an even number of parameters, the final parameter can be the default value. Each parameter can be an expression, though unlike ternary op (x ? y : z) all parameters would be evaluated; this is important if the expressions have side-effects.


A hotvar feature would affect performance of all scripts that use variables, without much reason. Something (script) has to change those variables; it may as well call the appropriate subroutines/functions directly.

Also, it is off-topic and has been suggested before.

Tuncay n-l-i mobile
  • Guests
  • Last active:
  • Joined: --
i suggest not to fall through automatically. For those who need this feature, continue could do this. May be also something similiar like "continue casename" could jump in the middle of current case to any case.

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
+1 for continue casename

fallthrough is useful and confuses newbies, but I would support continue casename OR fallthrough, but we need one or the other.

Of course, continue casename is basically a goto, so if the cases are specially formatted labels, continue casename could internally just do a goto.

then the labels would need to be constructed by the switch/select command.



this makes me think of a nice workaround:
value := 1

If IsLabel( "Case_" . value)
  Goto Case_%value%
Else
  Goto Case_Default

Case_1:
 Msgbox, this is case 1
  ;this syntax supports fallthrough


Al it takes to make this clean is replacing the IsLabel block with
Select, CasePrefix, Value, and to make Continue, casename a synonym for Goto.

or, even better:

Select(value, prefix="") {
If IsLabel( "Case_" prefix . "_" . value)
  Goto Case_%prefix%_%value%
Else
  IsLabel( "Case_" . prefix. "_Default")
    Goto Case_%prefix%_Default
}

Select_Continue(casename="", prefix="") {
If casename and IsLabel( "Case_" prefix . "_" . casename)
  Goto Case_%prefix%_%casename%
}

This is Stdlib compliant, and can actually be pretty usable I think. It doesn't support ranges of numbers, but that might not be a huge hurdle for basic use.

Select_Continue("", "prefix") would continue at Case_prefix_:

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
One thing that comes to mind is VB select case
which allows greater less than and other comparisons

Handy but perhaps not necesary

one idea comes from ListAHKFunctions.ahk by Philho
perhaps create an aray of label names then for comparisons other than = one could compare against the label names
Never lose.
WIN or LEARN.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
"Continue CaseName" was just a brainstorming idea. The suggestion is more to have Command "Continue" without parameter, which jumps just to next case.
Without break we have less to type, which is one of the goals of AHK. I think most of us would do most cases without falling through and it should be definitively done only explicitly by the user with continue. Break could be used to leave out the Switch...case construct completly.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I am against the use of continue with switch/select. It is really nothing more than a goto - overloading continue in this way would split its meaning and reduce the usefulness of switch/select in combination with a loop. Allowing break to be used for exiting the switch/select construct would similarly reduce its usefulness, and would negate one reason not to implicitly fall from each case into the next.

I think that allowing "goto case exact-expression-text" would be more intuitive. It cannot conflict with a label since no label can begin with "case" . A_Space. For simplicity and because case "labels" must be local to a switch/select construct, the target label/line would be resolved at load-time. Goto's argument must begin with "case " and %-derefs would not be supported.

engunneer: Your "stdlib" example will not work because any "Goto whose target is outside the function is ignored."

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
Thanks Lex,

I almost tried last night, but decided sleeping was important. I'll RTFM a bit and see if I can hack something together. I apparently missed that dynamic function calls were standard now, until tank told me at lunch. I'll have to see if I can wedge that into being useful here too.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007

until tank told me at lunch.

Yea but i was wrong in that it was implemented in v1.0.47.06 and i argued over lunch that it wasnt :cry: :oops:
Never lose.
WIN or LEARN.

Dannace
  • Members
  • 1 posts
  • Last active: Mar 27 2010 01:27 AM
  • Joined: 26 Mar 2010
In case anyone is actually still interested, there's a database language that few people have heard of called Clarion that has a great syntax for switch statements (in fact it has great syntax for everything):

Case val
Of 'A' :
DoStuffHere
OrOf 'B':
DoMoreStuff
Of 'C':
YetMoreStuff
Else:
DefaultStuff
End

There is no fallthrough by default and the 'OrOf' forces fallthrough.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
@Dannace
This does not help us in our AutoHotkey environment.

No signature.


argneo
  • Members
  • 220 posts
  • Last active: Mar 20 2015 09:18 PM
  • Joined: 14 Sep 2007
Were are we with these?
Posted Image Search&Replace

Don't use cannon to kill mosquito