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.,
is equivalent to@a = ("1", "2", "3"), "4", ("5", "6"), (), "7";
.@a = qw(1 2 3 4 5 6 7)
here's a one-liner that demonstrates that map returns empties:
and one the demonstrates that grep doesn't 1:perl -e '@foos = map { /[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 = grep { /[13579]/ ? "foo" : "" } (0..50); print join " +\n", @foos; print "\n\n"'
. .perl -e '@foos = map { /[13579]/ ? ("foo",$_) : () } (0..50); print jo +in "\n", @foos; print "\n\n"'
|
|---|