in reply to Re: Re: foreach/map equivalency
in thread foreach/map equivalency

Updated in response to upstream update...

So, then, if it does, or can, iterate over the source list in order to create the destination list, how is it that I am wrong?

I'm not trying to be right, I'm trying to gain an understanding of why I'm not.

Replies are listed 'Best First'.
Re: Re: Re: Re: foreach/map equivalency
by chromatic (Archbishop) on Nov 12, 2001 at 08:36 UTC
    No push necessary:

    my @times_two = map { $_ * 2 } (1 .. 12);
    That doesn't mean you can't use a push, just that you don't have to to make one list:

    my @duplicate; my @times_two = map { my $val = $_ * 2; push @duplicate, $_; $_ } (1 . +. 12);

    Does that make it clearer?

      I'm lost. Upstream a ways, you said "This is wrong." What, precisely, is wrong? I thought, apparently mistakenly, that you were taking exception to my statement that map() iterates over the list... now, it seems that it was my assertion that the two cases were not equivalent because the second didn't use push() as the first did...

      I'm in a maze of twisty little passages...

        The suggestion to use push in the map block was wrong. That's all. (In my opinion, explaining map as a synonym for for doesn't help people understand what map actually does.)

        Part of the confusion is that the original poster needed grep anyway.