Because the 2 code snippets arent quite the same. A better comparison would be
open(IN, "/some/file") || die "Cant open /some/file: $!\n";
while (<IN>) {
push(@foo, $_) if ( m/^$match/ );
}
close(IN);
# as opposed to
open(IN, "/some/file") || die "Cant access /some/file: $!\n";
@foo = grep(m/^$match/, <IN>);
close(IN);
The differences in the first post between the loops are as follows:
In the while loop, it continues to iterate over the input list, each time storing the value if it matches the regex. So if you have 3 values which match, then the last value matched will be placed in the variable.
In the grep loop, due to my excessive use of parentheses I forced grep to return a list, but only captured the first element. Along the same lines as say
$f = 'foo:bar:baz';
($blah) = split(/:/, $f);
$blah now contains 'foo', bar and baz are silently discarded. So grep finds all the matches, and returns them as a list, but I only grab the first value and toss the rest away. Use the first snippet in this post as a better example of the differences. Also remember that I stated on files themselves, not on directories, as grep really begins to out perform the loop as the data set gets larger. The loop gives you a far greater granularity of control in what to do with the contents of what you are iterating over. It will be more efficient to loop over a data set once in a loop if you are planning on doing different things with different pieces of the data. If you already know you want a specific piece of data, and only need to process the data set once, then grep is your friend.
MMMMM... Chocolaty Perl Goodness.....
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.