Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Hello Monks, 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? Thanks Monks! 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:
In reply to [Solved] How does map work? by three18ti
|
|