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!

How to write a switch statement in perl

Status
Not open for further replies.

kumar1986

Programmer
Jun 12, 2011
1
0
0
CH
Hello I have written a perl code using nested if statements but i want to use switch function insted of nestedif



#!/usr/bin/perl


print "Enter the Integers";

my $Integers =<>;

my @numbers =split(',',$Integers);

$numofint =scalar(@numbers );
@oldarray=0;
foreach my $num (@numbers)
{
is_inum($num);
print "@letters"
}



sub is_inum
{
print "$num\n";
my ($inum)=shift(@_);


if ($inum == 2)
{
@letters = (a,b,c);
return @letters;
}
elsif($inum == 3)
{
@letters = (d,e,f);
return @letters;
}


}





thanx in advance

BR,
kumar
 
If you only have 2 options
Code:
my @letters = $inum == 2 ? (a,b,c) : ( d,e,f)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
or you could do this which works for any number
Code:
my %option = ( 2 => [a,b,c], 3 => [d,e,f]);
my $isnum = '2';

for (@{$option{$isnum}}) {
 print "X:$_\n";
}

if you really want switches

but I've never used them.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
This is what I have been doing for years after reading it in Orielly "Perl Programming" book.

Code:
CASE: {
   @letters = (a,b,c), $var='abc', last CASE if ($inum == 2);
   @letters = (d,e,f), $var='def', last CASE if ($inum == 3);
   @letters = (x,y,z);
}

return(@letters);

Note the following:

The word "CASE" is just any arbitrary word I used. I could have used "SWITCH" or any other word. The word is just the name of the block of statements enclosed between the curly brackets.

I set $var just to illustrate that the scope of the if conditions applies to the 3 statements that are separated by a comma. If the usual semi-colon is used, the scope of the if statement will apply only to the "last CASE" statement.

The "last" statement exits out to the "CASE" block, when the if condition is true. If neither of the if conditions is true, control falls to the last statement, @letters = (x,y,z).

Although this construct looks cleaner than an "if/elsif/else/" construct, it is a bit slower. I think the reduced performance is caused by the "last" statement.


 
Just a caveat regarding Damian Conway's Switch.pm module - I wouldn't use it in production code or anything important.
At work we had a large script that was using Switch.pm, and it started causing some bizarre problems when I was making some updates to the code. It took a while to figure out what the problem was.

There's a warning in the documentation about bugs, and it was removed from the core module list in Perl 5.14

For simple stuff I haven't had any problems with it. I should add that the code that was having problems with Switch.pm was very poorly written, so that could have something to do with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top