Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Cases with the same Case Block in Switch Statements

Status
Not open for further replies.

midnightswan

Programmer
Nov 11, 2002
7
0
0
AU
Hi,

I was wondering if I can do this in Perl (multiple cases attached to 1 case block:

Code:
use Switch;

switch($var)
{
     case 'i'
     case 'j'
     {
        #... some code ...
     {
     case 'x'
     case 'y'
     {
        #... more code ...
     }
}

I know it can be done in like C++, Java etc, but I can't get it to work in Perl. I tried reading the info about the "Switch" module in CPAN, and it says this:

a series of case statements can try different types of matches -- hash membership, pattern match, array intersection, simple equality, etc. -- against the same switch value.

Link to CPAN ---> CPAN.org Switch Module Page

That section of text I just quoted is just under the 3rd grey box down the page.

I hope someone can help me : o)

Thanks in advance,
MS
 
Code:
use Switch;

switch($var) {
     case /ij/ {
        #... some code ...
     }
     case /xy/ {
        #... more code ...
     }
}
HTH
--Paul

cigless ...
 
Code:
#!/usr/bin/perl -w
use strict;
use Switch;

my $var = shift || "";

switch ($var) {
     case /^[ij]$/ { print "i or j\n"; }
     case /^[xy]$/ { print "x or y\n"; }
}


Trojan.
 
Thanks for your reply guys, but I'm a noob to Perl and am still having a hard time interpreting all the symbols and that correctly, so could you please explain what you assigned to the $var variable, and what the symbols mean in the case statements, i.e /^[ij]$/

Code:
#!/usr/bin/perl -w
use strict;
use Switch;

my $var = shift || "";

switch ($var) {
     case /^[ij]$/ { print "i or j\n"; }
     case /^[xy]$/ { print "x or y\n"; }
}
 
Code:
case /^[ij]$/ { print "i or j\n"; }
written as
Code:
case 
         /^[ij]$/ #[COLOR=red]the square braces indicate options, ie i or j, 
                  #the ^ means start of variable, and $ means
                  #end of variable.  So i or j are allowed, but not in
                  #conjunction with anything else[/color]
         { print "$var is i or j[$var]\n"; }
                  [COLOR=red]#this you can work out ;-)[/color]
The problem with the code I posted was I wasn't allowing for options to $var would have to be equal to ij to drop into that code block.

Hope that makes things clearer, and apologies for misleading you

--Paul
 
thanks :eek:) I understand the case bit now. I'm now having problems getting it to work for my code. This is it here:

Code:
sub Test
{
    #Assigning subroutine parametrs to meaningful variables
    my $session = $_[0];
    my $trunkMode = $_[1]; 	
		
    switch($trunkMode){
        case /^[ringNone endPtNone]$/
        {
              ...code...
        }
        case /^[ringUPSR_SNCP midSpanNoProtection]$/
        {
              ...
        }
}

Is that correct?? I've also tried
Code:
case /^['ringNone' 'endPtNone']$/
and
Code:
case /^["ringNone" "endPtNone"]$/

and none of them seem to allow me to switch into the cases
 
The [ and ] are for a character class of one character only.
You have changed the ground rules by asking how to switch on a single character string and then expeted it to work with multi-character strings.
To make that work try this:
Code:
/^(ringNone|endPtNone)$/
If you want to be really clean, do this:
Code:
/^(?:ringNone|endPtNone)$/
You'll find that in forums, it's best to ask for exactly what you want. If you try to use examples as you did then you are likely to get something very different.

I hope that helps.

Trojan.
 
I'm sorry, I'll know not to do that again. :)

Thanks heaps for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top