in reply to Re: Re: "foreach" is to "next" as "map" is to ???
in thread "foreach" is to "next" as "map" is to ???

grep is just a specialized form of map

Not entirely true.

The difference between grep { TEST } and map { TEST ? $_ : () } is that grep returns a list of aliases to the elements that match TEST. map doesn't do this for obvious reasons - its return can be anything, it's in no way bound to $_.

For example, if you have an array of numbers and you want to set any negative elements to zero, you can do it with the following code (of course this is not the most common way):

$ perl @ary = (1, -5, -12, 7, 3); $_ = 0 for grep {$_ < 0} @ary; print "@ary\n"; ^D 1 0 0 7 3