Perl-Sensitive Sunglasses | |
PerlMonks |
Re: playing with mapby archon (Monk) |
on Mar 09, 2001 at 13:15 UTC ( [id://63203]=note: print w/replies, xml ) | Need Help?? |
map and grep are two different functions for a reason.
map basically says "take this list, do something to every element in it, and give me the resulting list." grep basically says "take this list, find all elements for which this expression is true, and give me the resulting list." in general you don't want to use either of these functions in a void context, i.e. you wouldn't run them without assigning the list they return to some variable. also, you wouldn't want to use them unless you want to iterate through the entire list. in either of these situations, a foreach loop (with a call to last in the second situation) is more appropriate. your realization and reconstruction of the map usage to assign the results of the operation to @n was correct. you couldn't do it with a grep call, because grep returns some subset of the original list. it doesn't make any modifications. also, as you said, why would you want to? Update:Whoops.. guess i should have re-read the grep info before i made that last statement (=
In Section
Seekers of Perl Wisdom
|
|