No such thing as a small change | |
PerlMonks |
Re: How would I write this using map?by monarch (Priest) |
on Jan 13, 2009 at 00:38 UTC ( [id://735842]=note: print w/replies, xml ) | Need Help?? |
The map is used for performing an action on every item in an array, with the result of that action returned.
In this case it appears that you want to push (in order) each key from the hash reference $obj->things. As you're not performing a transformation the quickest thing to do is to push the list directly onto the array:
However map could be used to re-write your loop as: which is identical to However using map here isn't so common because nothing is being done with the output of map. You're merely using map to iterate through each item (which is a valid use, though). What if you wanted to, say, store an uppercase-version of all the keys into the array? Then map becomes very useful: This is much quicker than, but functionally the same as:
In Section
Seekers of Perl Wisdom
|
|