Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: how to comment the lines if found a match

by kavitha (Initiate)
on Dec 05, 2008 at 21:49 UTC ( #728395=note: print w/replies, xml ) Need Help??


in reply to Re: how to comment the lines if found a match
in thread how to comment the lines if found a match

This is how my program looks like now. I tried this but the grep here is not returning right value, its returning 1 as if its matching all the time.

#!/usr/bin/perl -w
open(MATCH_PATTERN,"< temp1");
open(FILE_TO_LOOK,"< temp2");
open OF, "> temp3";

while ($m =<MATCH_PATTERN>)
{
print "reading the current text to match \n $m \n";
seek (FILE_TO_LOOK ,0, 0);
while ($l= <FILE_TO_LOOK>) {
print "reading the textline to look for match \n $l \n";
@found = grep ($m, $l) ;
$match = @found;
if ($match == 1)
{
print "now the match is \n $match \n";
print OF "#$l";
}

}
}
close FILE_TO_LOOK;
close MATCH_PATTERN;
close OF;
  • Comment on Re^2: how to comment the lines if found a match

Replies are listed 'Best First'.
Re^3: how to comment the lines if found a match
by gwadej (Chaplain) on Dec 05, 2008 at 21:59 UTC

    You are misunderstanding the use of grep.

    In your example, grep will always return $l unless $m happens to be 0 or an empty string.

    Instead of grep, you probably want a regular expression.

    if($l =~ /\Q$m\E/) { print "We matched\n"; print OF "$$l"; }

    You probably also want to use chomp in the outer loop to remove the line ending from $m.

    You are also writing the file multiple times with different lines commented each time. You might want to generate a list of the expressions to match at the beginning and then loop over the file once.

    G. Wade

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://728395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2023-11-30 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?