in reply to (jeffa) Re: phrase and word searching
in thread phrase and word searching

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:

but i'm digressing again.

.