in reply to foreach/map equivalency

map iterates over the array, as you suspected it would... in the case you provide, you use a push in the first example, but don't in the second. So, you're seeing the default, Larry-thought-it-wise behavior in the second.

Rewrite that map statement so that it, too, uses push, and it oughta do what you expected.

Replies are listed 'Best First'.
Re: Re: foreach/map equivalency
by chromatic (Archbishop) on Nov 11, 2001 at 02:27 UTC
    This is wrong. map produces a list without having to perform any stack or array operations. That's why it's not spelled for or foreach. That's also why rob_au is getting null values.

    Update: That is to say, map handles the list construction for you, and assigning the results to some sort of variable handles the list assignment. Under the covers, it's free to use whichever linked list or stack operations it prefers.

      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.

        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?