Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

RE: My favorite looping mechanism in Perl is:

by marcos (Scribe)
on May 10, 2000 at 13:05 UTC ( #10921=note: print w/replies, xml ) Need Help??

in reply to My favorite looping mechanism in Perl is:

I agree with BBQ: I love foreach, but when I discovered map ... it was love at first sight :) being able to write things like:
@words = ('foo', 'bar', 'and', 'whatelse'); map {print "$_\n"} @words;

is a real pleasure for me!

Replies are listed 'Best First'.
RE: RE: My favorite looping mechanism in Perl is:
by merlyn (Sage) on May 13, 2000 at 04:30 UTC
    Don't use map in a void context. Now that the backwards foreach is in there, it's never necessary. And it's also wasteful.
      Thank you for your note, but could you please explain why using map in void context is "a wasteful"? Is it a problem of code performance and/or optimization?
      TIA, marcos
        I believe it is/was wasteful because of the list that map builds to return to you. The normal use of map is like this:
        my @returned = map BLOCK LIST;
        In order to fill up @returned, map has to build a list. If you're not going to be using that list (if you're using map in void context), you've wasted that storage and the processing time needed to allocate that memory.

        However, I've read bits and pieces of this discussion in the past, and I had thought that this list-building was optimized away when map is used in a void context. Is this the case, or was that merely a suggestion? :)

RE: RE: My favorite looping mechanism in Perl is:
by ChuckularOne (Parson) on May 10, 2000 at 15:43 UTC
    map! Map! MAP! I love it. I have never used it, and now I'm thinking of all of the extra lines of code I've written over time to do what a simple map does!!!

    Thank you!
    I think I have a new favorite widget for the week!

    Your humble servant,
      This is the same reaction I had when I discovered map :-) I'm not sure if it is a real looping mechanism (technically speaking): if you check the manpage it says "map - apply a change to a list to get back a new list with the changes". Anyway I think it is very powerful, and very beautiful, and I use whenever I can!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://10921]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2016-09-28 21:13 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (540 votes). Check out past polls.