LuckyLucks
asked on
pattern matching in perl
Hi
Is there an elegant way to pattern match a couple of patterns without having several or ( pattern1 || pattern2 || pattern3) in your code , specially when the $1,$2 matched patterns could differ based on which pattern actually matched.
I am trying to avoid having too many if -else - or else statements.
eg:
if (string=~pattern1)
else if(string=~pattern2)
else if(string=~pattern3)
etc.
thanks
Is there an elegant way to pattern match a couple of patterns without having several or ( pattern1 || pattern2 || pattern3) in your code , specially when the $1,$2 matched patterns could differ based on which pattern actually matched.
I am trying to avoid having too many if -else - or else statements.
eg:
if (string=~pattern1)
else if(string=~pattern2)
else if(string=~pattern3)
etc.
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, depending on what you want to do, you may be looking for Switch (http://perldoc.perl.org/5.8.8/Switch.html) or given (http://perldoc.perl.org/perlfaq7.html#How-do-I-create-a-switch-or-case-statement%3f). Switch was part of the core until 5.10 or 5.12 (it can still be installed from CPAN). given was added in 5.10 (I think).