in reply to Map: Thou Has't Confounded Me For The Last Time! (Referring To The Array You Are Assigning To In Map)

There was a detailed thread on this before. I don't have time to search for it right now, but it was determined that

%hash = map { getkey($_) => $_ } @array;

is closer to

@anon_temp = (); foreach (@array) { push(@anon_temp, getkey($_), $_); } %hash = @anon_temp;

than to

%hash = (); foreach (@array) { $hash{getkey($_)} = $_; }

and that the %hash = @anon_temp; accounts for the speed difference.


As for your other question, my $something = $value if $something is similar to (or the same as?) (my $something = $value) if $something. The my in executes only if $something was true before this statement. Never use if, unless, etc on a my.

  • Comment on Re: Map: Thou Has't Confounded Me For The Last Time! (Referring To The Array You Are Assigning To In Map)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Map: Thou Has't Confounded Me For The Last Time! (Referring To The Array You Are Assigning To In Map)
by halley (Prior) on Feb 14, 2005 at 14:46 UTC
    I feel that ikegami has provided the most accurate explanation of the map operator.

    This is the beginning of the road to understanding the difference between a list and an array, by the way. You'll see both terms in the perldocs, but you won't see a lot to explain the difference. An array is a variable type. A list is what an array contains, but lists often exist without any array at all. The @anon_temp in ikegami's example is one way of explaining such a list.

    Note that foreach is a statement type, while map is an operator. This operator takes one list, and transforms it into a completely new list, element by element.

    --
    [ e d @ h a l l e y . c c ]

      For future reference, there is a good explanation in the perldocs. It's in perlfaq4, accessible directly via perldoc -q 'list and':

      Found in /usr/share/perl/5.8/pod/perlfaq4.pod What is the difference between a list and an array? # contents snipped to avoid massive annoyance
Re^2: Map: Thou Has't Confounded Me For The Last Time! (Referring To The Array You Are Assigning To In Map)
by ikegami (Patriarch) on Feb 14, 2005 at 15:24 UTC