and, as a feature of map, doesn't return the 'empty' ones
er, um. map does return empties; grep does not. however, i think what's going on in that case is that map is returning an empty list, which then gets "lost" in the assignment into a list, because of how arrays flatten in assignment. e.g.,
@a = ("1", "2", "3"), "4", ("5", "6"), (), "7";
is equivalent to
@a = qw(1 2 3 4 5 6 7)
.
here's a one-liner that demonstrates that map returns empties:
perl -e '@foos = map { /[13579]/ ? "foo" : "" } (0..50); print join "\
+n", @foos; print "\n\n"'
and one the demonstrates that grep doesn't
1:
perl -e '@foos = grep { /[13579]/ ? "foo" : "" } (0..50); print join "
+\n", @foos; print "\n\n"'
... and that lists get squashed if map happend to be returning lists:
perl -e '@foos = map { /[13579]/ ? ("foo",$_) : () } (0..50); print jo
+in "\n", @foos; print "\n\n"'
.
[1] well, it isn't as easy as that. grep returns a list of the values on the rhs that make the grep evaluate to true. but if you can use it like a map by assigning into $_, if you really want to:
perl -e '@foos = grep { $_= (/[13579]/ ? "foo" : "") } (0..50); print
+join "\n", @foos; print "\n\n"'
but i'm digressing again.
.
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.