ERROR.. with regex!!

D

darkname

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!?

Tank you all
 
J

Janek Schleicher

darkname wrote at Tue, 15 Jul 2003 09:41:52 +0000:
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!?

I suppose that $comm has some special characters inside, that are
interpreted in regexes. In this case, they seem to create a "Nested
quantifier ..." error (perhaps there is something like ?*? or so inside).

You switch this behaviour completely off with automatic quoting of the
variable content:

/\Q$comm/ instead of only /$comm/, but I'm not sure whether you want it
:)


Greetings,
Janek
 
J

James E Keenan

darkname said:
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"....
Once I pre-declare all the variables, the code compiles properly under
use strict and use warnings. (You are using these pragmata, right?)
Hence, I would infer that you have a run-time problem. My first
suggestion would be that you check the value of $comm at the line
where you get the error. Perhaps characters are being interpolated
here which give the error.
 
N

nobull

darkname said:
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!?

The file from which @com is loaded contains somewhere the sequence
'**'.

Perhaps that's just a mistake in the file.

Perhaps you didn't tell the person who provided the file that you were
expecting a file full of (Perl) regular expressions.

Trying to interpret something that is not a valid regular expression
as a regular expression can lead to errors.

Actually this is not specific to regular expressions - it applies
equally to arithmetic expressions, SQL statemnts, Perl statements,
sentences in French... whatever. In general, trying to interpret
something that is not a valid[whatever] as a [whatever] can lead to
errors.

If you never indended that the file would contain regular expression
then you need to learn out about the \Q interpolation escape (also
known as the quotemeta() function) and/or the index() function.

See also posting guidelines.
 
J

John W. Krahn

darkname said:
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!?

The problem is that the contents of $comm contains characters that are
special to regular expressions. Use index() instead:

foreach my $line ( @fich ) {
$i++;
foreach my $comm ( @com ) {
if ( index( $line, $comm ) >= 0 ) {
print "Line $i: $comm";
}
}
}


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top