in reply to Map Function Weirdness

I, also, am a bit unsure of the real question of rthawkcom.

However, if it has to do with the fact that the  @out array has all the values of the  %hash when  $_; is the last statement executed in the map block and has a few empty strings in place of hash values when  $_; is not the last statement, I think I can explain.

The value of the  map block on each iteration will be the value of the last expression evaluated in the block. This is always the  $_ expression, previously set to a hash value, when the  $_; statement is last.

When the
    if($_>$max){$max=$_}
statement is last,  $_>$max will be the last expression evaluated when  $_ is less than or equal to  $max (and it will evaluate to the empty string, or false).

When  $_ is greater than  $max the  $max=$_ true clause of the statement will execute, which evaluates to  $_ previously set to a hash value.

Update: This reply is essentially the same as jethro's.

Replies are listed 'Best First'.
Re^2: Map Function Weirdness
by ikegami (Patriarch) on Nov 08, 2011 at 22:01 UTC

    and it will evaluate to the empty string

    Not quite. The comparison operators and possibly all operators returns triplevar(0, 0.0, "") for false.

    $ perl -wE'say 0 + (0 > 1)' 0 $ perl -wE'say 0 + ""' Argument "" isn't numeric in addition (+) at -e line 1. 0

    which evaluates to $_ previously set to a hash value

    Not quite. Scalar assignment evaluates to its LHS, so it evaluates to $max, not $_.

    $ perl -E'$_ = 123; ++( $max = $_ ); say $max; say $_;' 124 123

    Of course, they hold the same value at that point, and map promptly copies that value.

Re^2: Map Function Weirdness
by rthawkcom (Novice) on Nov 08, 2011 at 20:16 UTC
    It is, but easier to follow. I voted you both up. Thanks for the fast help!