[Wellington-pm] At the buzzer

Grant McLean grant at mclean.net.nz
Sun Aug 22 01:59:12 PDT 2010


On Fri, 2010-08-20 at 14:35 -0400, pfCd?Y*_Qbdb6 at perform.shyuser.com
wrote:
> Any monger like to comment why these expressions produce the 
> same result?
> 
> use List::Util qw(sum);
> 
>    $product1 = average_5d(@split_line);
> 
>    sub average_5d {
>        my $sum = sum map { $_->[7] } @_[1..5];
>        return $sum / 5;
>    }
>
> #Next the expression was bumped a day [2..6] to produced a
> #comparison between $product1 and $product1A .
>
> use List::Util qw(sum);
> 
>    $product1A = average_5d(@split_line);
> 
>    sub average_5d {
>        my $sum = sum map { $_->[7] } @_[2..6];
>        return $sum / 5;
>    }

Without knowing what numbers are in @split_line, it's not possible to
say.

When you say those two routines return the same result, you're not
putting them in the same script are you?  If you put those two code
snippets in the same script then the 'average_5d' subroutine would get
defined twice and the second definition would replace the first one.
This happens during the compilation phase, before any of the lines are
executed, so when this line ran:

  $product1 = average_5d(@split_line);

it would be executing the second version of average_5d, not the first.

The solution would be to give the second subroutine a different name.

Of course if I've guessed wrong we'd really need to see the data.

Cheers
Grant




More information about the Wellington-pm mailing list