Solved

Is there a equiv. for SELECT CASE or SWITCH

Posted on 1997-12-22
10
928 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

828 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