nofernandes has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code!!
foreach $line (@fich){ $i++; foreach $comm (@com){ if(($line eq $comm)||($line=~/$comm/)){ print"Line $i: $comm"; } } }

The problem is that the instruction in the if condition ($line=~/$comm/) gives me an error!!! "Nested quantifiers before HERE mark in regex m//** << HERE"....

The @comm and @fich arrays contain the contents of two distinct files!

Can someone tell me why this code gives me this error!?

Thank you all

Replies are listed 'Best First'.
Re: Regex ERROR!
by broquaint (Abbot) on Jul 15, 2003 at 09:44 UTC
    You'll need to escape the meta-characters in $comm e.g
    ## note the \Q before $comm if(($line eq $comm) || ($line =~ /\Q$comm/)){ print"Line $i: $comm"; }
    And it looks like a simple index would be better suited in this situation since you're just testing to see whether $comm is in $line e.g
    if(($line eq $comm) || (index($line, $comm) > -1)){ print"Line $i: $comm"; }
    See. perlre and quotemeta for more info on escaping regex meta-characters and perldiag for a more detailed explanation of your error.
    HTH

    _________
    broquaint

Re: Regex ERROR!
by Skeeve (Parson) on Jul 15, 2003 at 09:47 UTC
    have you tried /\Q$comm/ ? I fear it's a problem with the content of $comm.
      Thank you Very much!!!
      In did the \Q was the problem!!!

      It might be a stupid question but what does the \Q does in this context??

      Thank you again for your help!!

      Has in my country they said "The one that doesn´t know is like the one that cannot see!!"

        from the perlre man page
            \Q    quote (disable) pattern metacharacters till \E
        
        look at this code:
        $comm = '(xyz'; $line = '123(xyz)456'; if ($line =~ /$comm/) { #error! same as $line =~ /(xyz/ which ha +s an unmatched '(' # do stuff } if ($line =~ /\Q$comm/) { #no error! same as $line =~ /\(xyz/ -- ' +(' is escaped # do stuff }

        --

        flounder