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

open IN, "d1.txt" or die "No such file:$!\n"; open OUT, ">ABC.txt" or die; while(<IN>) { chomp $_; $a[$l]=$_; ++$l; } my @array=qw(doxorubicin is a potent anti-estrogen drug and tamoxifen) +; for($i=0;$i<$l;++$i) { foreach $z(@array) { if($z=~m/\b\w^$a[$i]$\b/i) { $z=~s/$z/<span style="background-color:yellow;">$z<\/span>/; $i++; print "@array "; } } }
My script is fine, its just giving range error while matching with terms like below:
(5r)-6-(4-{[2-(3-Iodobenzyl)-3-Oxocyclohex-1-En-1-Yl]Amino}Phenyl)-5-M +ethyl-4,5-Dihydropyridazin-3(2h)-One show range error at 2-(
Can somebody tell me how to turn off the effect of such characters like brackets, range operator etc

Replies are listed 'Best First'.
Re: Invalid range []
by jwkrahn (Abbot) on Dec 12, 2011 at 10:02 UTC

    Your code as posted:

    open IN, "d1.txt" or die "No such file:$!\n"; open OUT, ">ABC.txt" or die; while ( <IN> ) { chomp $_; $a[ $l ] = $_; ++$l; } my @array = qw( doxorubicin is a potent anti-estrogen drug and tamoxif +en ); for ( $i = 0; $i < $l; ++$i ) { for $z ( @array ) { if ( $z =~ /\b\w^$a[$i]$\b/i ) { $z =~ s/$z/<span>$z<\/span>/; $i++; print "@array "; } } }
    My script is fine,

    I don't see how you can say that.

    You are trying to use the string "(5r)-6-(4-{[2-(3-Iodobenzyl)-3-Oxocyclohex-1-En-1-Yl]Amino}Phenyl)-5-Methyl-4,5-Dihydropyridazin-3(2h)-One" as a regular expression to match against the contents of @array but the elements of @array are all smaller than that string so they will never match.    The regular expression has to be able to fit inside the string.

    You are using the regular expression /\b\w^$a[$i]$\b/i which says: Match a Word boundary followed by a single Word character followed by the beginning of the string followed by the contents of $a[$i] followed by the end of the string followed by a word boundary.    You can't match anything BEFORE the beginning of the string or AFTER the end of the string, there is nothing there to match.

Re: Invalid range []
by moritz (Cardinal) on Dec 12, 2011 at 05:41 UTC