Welcome to the Monastery | |
PerlMonks |
A hint about using mapby doom (Deacon) |
on Apr 05, 2005 at 23:17 UTC ( [id://445124]=note: print w/replies, xml ) | Need Help?? |
One of the things you always want to do with map is to use it to do a transformation on a list of strings.
At some point you're guaranteed to try something like this, but it won't work: This is because s/// returns the *count* of the changes, rather than the changed string (most people agree this is a DWIM violation: this is an area where Larry blew it). What actually works is something like this:
There's still another gotcha here though, in that if you look at the original @list after doing this, you'll find that it was transformed in exactly the same way as the new list. Each item in the @list array is aliased to $_, so it can be changed by running map on it. (Of course, you knew this. But you'll still get bitten by it on occasion.) Weirdly enough, the m// operator doesn't have the same problem as s/// does: m// does pretty much what you'd expect in list context:
Trivia: perl is well known for having ripped off features form shell and awk, but map was lifted from lisp.
In Section
Seekers of Perl Wisdom
|
|