Solved

Is there a equiv. for SELECT CASE or SWITCH

Posted on 1997-12-22
10
922 Views
Last Modified: 2008-03-10
I'm searching for the perl equivalent of the VB command SELECT CASE or the identical SWITCH command in C.
0
Comment
Question by:Kobe_Lenjou
10 Comments
 
LVL 2

Expert Comment

by:haystor
ID: 1209380
There is no direct equivalent, but something close can be constructed using if-elsif-else...
for instance:

if ($a eq "a") {
   print "a\n";
} elsif ($b eq "b") {
   print "b\n";
} else {
   print "it was neither a nor b\n";
}

The equivalent of fall throughs are best constructed using multiple if's altough the conditionals can become complicated.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1209381
I prefer:

SWITCH: {
    $_=$a;
    /^a$/ && do { ..; last SWITCH; };
    /^b$/ && do { ..; last SWITCH; };
    # default follows here
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1209382
or:

for( $a ){
  /^a$/ && do { ..; last }
  /^b$/ && do { ..; last }
  # default follows here
}

or even:

%switch=(
'a'=>sub{print "a\n"},
'b'=>sub{print "b\n"},
);
&{$switch{$a}||sub{print "it was neither a nor b\n"}}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1209383
BTW, Kobe_Lenjou, got a server error on your question qid=8630031117,
and couldn't read the last comment you added there.
0
 

Expert Comment

by:mgjv
ID: 1209384
to find more possibilities to do the same thing, simply read the perlsyn documentation that comes with perl.

perldoc perlsyn

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Author Comment

by:Kobe_Lenjou
ID: 1209385
I like the answer of ozo and I want to give you the credit for this one.

0
 
LVL 8

Author Comment

by:Kobe_Lenjou
ID: 1209386
Note to ozo,i get the server erorr myself :(
0
 
LVL 84

Expert Comment

by:ozo
ID: 1209387
Which answer?  As mgjv says, most of these suggestions, and several more, are given in perlsyn.
(The &{$switch} method is not given in perlsyn, but it can be inferred from perlref.)

If qid=8630031117 has gotten lost, you may want to email linda@experts-exchange.com
or post a question to the customer service area to recover your points
so you can post that question again.
0
 
LVL 1

Expert Comment

by:hank1
ID: 1209388
<<__comments__;

Here's a switchy thing I use.  By-the-way I understand switch
is on its way.

__comments__


$switchee = 'b';
SWITCH: {
  if($switchee eq 'a'){
    print "Is a.\n";
    last SWITCH;
  }
  if($switchee eq 'b'){
    print "Is b.\n";
    last SWITCH;
  }
  if($switchee eq 'c'){
    print "Is c.\n";
    last SWITCH;
  }
  if($switchee eq 'd'){
    print "Is d.\n";
    last SWITCH;
  }
  print "Is not a, b, c or d.\n";
  last SWITCH;
}



0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1209389
for( $a ){
  /^a$/ && do { print "a\n"; last }
  /^b$/ && do { print "b\n"; last }
  # default follows here
  print "it was neither a nor b\n";
}
#or
%switch=(
                        'a'=>sub{print "a\n"},
                        'b'=>sub{print "b\n"},
);
&{$switch{$a}||sub{print "it was neither a nor b\n"}}  
#or see perlsyn or perlfaq7
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now