?
Solved

Is there a equiv. for SELECT CASE or SWITCH

Posted on 1997-12-22
10
Medium Priority
?
933 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

718 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