You can do two things here: Either approach the problem the same way as the shell script did, and search the file twice, once for matching lines, and once for nonmatching lines, or you can search through the file once, and put the matching lines into one list, and the nonmatching lines into another list.
Your naming of variables is quite confusing - you shouldn't give the iterator of a loop a name and then not use it at all. I've given the iterator a different name than the list it walks through, this should make it clearer to you what is happening:
# untested snippet my @good_lines,@bad_lines; foreach my $file_name (@file){ open(MYFILE, $file_name) or die qq(Cannot open '$file_name' : $!\n); # Read the whole file into memory my @lines = <MYFILE>; # And find the good and bad lines push @good_lines, grep { ! /NOT|INVALID|MUST|ERROR/ }, @lines; push @bad_lines, grep { /NOT|INVALID|MUST|ERROR/ }, @lines; }; print "good: @good_lines"; print "bad : @bad_lines";
This way gives easy results, but it consumes quite a lot of memory, as every file is loaded twice into memory and we do twice as much work when searching through the lines, as every line that does not belong to the good lines must belong to the bad lines. We can change the script to follow this logic by doing it like this:
# untested snippet my @good_lines,@bad_lines; foreach my $file_name (@file){ open(MYFILE, $file_name) or die qq(Cannot open '$file_name' : $!\n); # Read file line by line my $line; while (defined $line = <MYFILE>) { # Does the current line match our RE? if ($line =~ /NOT|INVALID|MUST|ERROR/) { push @bad_lines, $line; # store it in @bad_lines } else { push @good_lines, $line; # store it in @good_lines }; }; }; print "good: @good_lines"; print "bad : @bad_lines";
Both methods I've shown you do not create the temp files ${good_ones} and ${bad_ones} - if you want/need this, you should print the lines directly into the respective file handles.
As an aside, you die whenever a file can't be opened for reading, but you don't tell the user why it couldn't be opened. You should use $! as the error reason in the string.
open(MYFILE, $file[$i]) or die qq(Cannot open $file[$i] : $!\n);
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
In reply to Re: Unix grep to Perl grep
by Corion
in thread Unix grep to Perl grep
by LeeC79
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |