Clear questions and runnable code
get the best and fastest answer
[Solved] How does map work?by three18ti (Scribe)
|on Oct 26, 2013 at 20:07 UTC||Need Help??|
three18ti has asked for the
wisdom of the Perl Monks concerning the following question:
I'm reading Higher Order Perl and one of the examples is an "imap" function which acts like map only it takes a block and an iterator.
That code may as well be in greek because it makes no sense to me. So I figured I'd try to write my own map function to see how it works.
But that produces the error "Use of uninitialized value $_ in exponentiation (**) at coderef.pl line 19."
I know when I pass parameters to a subroutine or closure they are passed in in @_, so I tried to shift the parameter off @_, but that produces the same error (based on my understanding of perl, this second attempt should work... "Should" is funny word).
What gives? How does map/grep work it's magic? And why doesn't my attempt work? I realize that all of the useful map functions have probably already been written, so this is more of an exercise in understanding, how could I write my own map function?
Edit: Seems my problem was with my use of while instead of for, I thought while set $_, but I guess not.
Here's the version that works: