AutoHotkey Community

It is currently May 26th, 2012, 4:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 45 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: April 14th, 2009, 11:13 pm 
Offline

Joined: December 28th, 2005, 10:46 am
Posts: 99
You can look at a recent topic here, for example...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2009, 12:16 am 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Chris Posted: Mon Jun 20, 2005 5:22 am wrote:
I think you're right that this is important. I'll try to get it done within the next 30 days.
or not
looks like a very long 30 days

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2009, 1:01 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3329
Location: Simi Valley, CA
Xystus777 wrote:
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:
Code:
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
Code:
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.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 7th, 2009, 8:59 pm 
Offline

Joined: January 22nd, 2009, 3:43 pm
Posts: 84
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:

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 7th, 2009, 11:24 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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:
Code:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2009, 11:40 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2009, 3:43 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
+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:
Code:

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:

Code:
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_:

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2009, 3:56 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
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

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2009, 6:58 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
"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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2009, 9:16 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2009, 12:31 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
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.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2009, 1:29 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
engunneer wrote:
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:

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 26th, 2010, 8:41 am 
Offline

Joined: March 26th, 2010, 8:33 am
Posts: 1
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 26th, 2010, 10:25 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
@Dannace
This does not help us in our AutoHotkey environment.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 6th, 2010, 3:13 pm 
Offline

Joined: September 14th, 2007, 4:51 am
Posts: 220
Were are we with these?

_________________
Image Search&Replace

Don't use cannon to kill mosquito


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 45 posts ]  Go to page Previous  1, 2, 3

All times are UTC [ DST ]


Who is online

Users browsing this forum: gamax92 and 1 guest


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