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!

Regular Expression Problems

Status
Not open for further replies.

dulux

Programmer
Sep 4, 2002
36
0
0
GB
Hi,

I am new to all this Perl stuff and am having a minor problem. Fact is, I am crap at regular expressions.

Here is an example of something I want to do, but cannot find the right combination of \/\/ or whatever to do it.

Here goes -

I have a variable that contains either \ or 0-9. If the variable is \ - do something, if 0,2,4,6,8 - do something else, if 1,3,5,7,9 - do something else.

I have tried matching the contents of the variable to an array containing these numbers without success, and am sure I am missing something. I also want to create something similar to a switch statement in 'C'. Is there anything similar in perl?

Nevertheless, I think perl is great and I will persevere...

Hope one of you guys can help, and have a Merry Christmas.

 
$var = '1359';

if ( $var =~ /\\/){
print "a backslash\n";
} elsif ( $var =~ /1|3|5|7|9/ ){
print "an odd digit\n";
} elsif ( $var =~ /0|2|4|6|8/ ){
print "an even digit\n";
}

There's not an actual "switch" statement in Perl, there *are* some approximations and this is the one I use.
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
I assume you DO NOT have something like

'73545\615\7399\0475\734'

What you seem to have is something like
my @array=('\susf',70,548,44,'\sdhjadak\tu');
foreach my $variable(@array){
if($variable=~/\\/){print "slashed $variable ! \n";
}elsif($variable=~/\d/){print "digit $variable\n";}
else{die "unexpected input $variable\n";}

}

perl has case

good luck, svar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top