On Thu, Mar 20, 2008 at 9:01 AM, Ram Prasad <[EMAIL PROTECTED]> wrote:
> I have a somewhat strange requirement
>  I want to find if a regex matched what exactly matched
>
>  to reproduce this
>
>  ------------------
>  my @x;
>  $x[0] = 'chi+ld*';
>  $x[1] = '\sjoke';
>
>  $_=getinput();               # for test assume $_="This is a joke";
>
>  if(/($x[0]|$x[1])/){
>        print "Matched '$1' \n";
>  }
>  -----------------
>
>
>  I want to know if $x[0] matched or $x[1] matched
>  What is the most efficient way of doing this ?
snip

Don't use an alternation:

if (/($x[0])/) {
    #first one matched
} elsif (/($x[1])/) {
    #second one matched
} else {
    #neither matched
}

Also, you should use the qr// operator* instead of quotes when storing
a regex in a variable:

my @x = (
    qr/chi+ld*/,
    qr/\sjoke/
);

* http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators
-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to