Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

map vs foreach

by dash2 (Hermit)
on Jun 20, 2003 at 17:07 UTC ( #267656=note: print w/ replies, xml ) Need Help??

in reply to Re: Re: Re: Re: Re: Using s///e and it just doesn't feel right
in thread Using s///e and it just doesn't feel right

but that uses the return from map, although it's hidden.

I don't see why using map in a void context is so wrong. Sometimes it expresses something better, particularly if the order of operation doesn't matter:

map {transform($_)} @elements; # transform() does something inline
A massive flamewar beneath your chosen depth has not been shown here

Comment on map vs foreach
Download Code
Replies are listed 'Best First'.
Re: map vs foreach
by jsprat (Curate) on Jun 20, 2003 at 17:45 UTC
    Is that more expressive to you than something like this:

    trasform $_ foreach @elements;

    If so, how?

      No, I quite like the single-line foreach also, but it has the disadvantage of being relatively recent... I think 5.005?? When I started using Perl I had to be _very_ cross-platform, and the habit has stuck.
      A massive flamewar beneath your chosen depth has not been shown here
Re: map vs foreach
by antirice (Priest) on Jun 20, 2003 at 17:26 UTC

    This first call to bob uses a map in void context whereas the second call does not. The point was to imply the question of whether I should use two different subs to perform the same task having only the difference that one returns a list and the other returns nothing.

    bunnyman: [In response to: And why then is map allowed to be called in void context?] There are a lot of things you can do with Perl that you shouldn't do.

    Following that logic, I should create two subs. Perl takes the stance of TIMTOWTDI. Why not let the programmer determine what should be used instead of laying down a draconian policy?

    dash2, this isn't directed towards you (considering we are basically on the same platform with this one). I'm just saying what I thought was obviously implied by the code.

    The first rule of Perl club is - use Perl
    ith rule of Perl club is - follow rule i - 1 for i > 1

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2015-11-30 20:33 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (782 votes), past polls