Solved

Is there a equiv. for SELECT CASE or SWITCH

Posted on 1997-12-22
10
923 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
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 …
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

22 Experts available now in Live!

Get 1:1 Help Now