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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.