[EMAIL PROTECTED] (John W. Krahn) writes:

> "Michael R. Wolf" wrote:

[buggy code deleted ...]

> $ perl -le'
> @fred = qw(1 3 5 7 9);
> sub total {
>     my $sum;
>     $sum += $_ foreach (@_);
>     }    
       # undef from final foreach always returned!!!!
> print total( @fred );
> '
>

My bad!  Sorry.  I'm working on a laptop (with about 5M, yes
M, extra disk space) away from my Perl environment.  No room
to set up the envorienment that I should have to prevent
posting buggy code.

The last "executed" expression was the failing loop
iteration, therefore, that's what was returned.  (I even
thought about it for a couple of minutes before I posted it!
And I got it wrong.  Nothing like running Perl through perl
(instead of my grey matter) to get the right answer.  I got
bit by my own hubris.  I tried to make it as small as
possible, but I made it _too_ small.  I got distracted by
checking the docs to confirm that the return value of an
incrementing assignment was the newly incremented value.  I
forgot to consider the final loop iteration.)

Add either of these lines before the closing brace to fix my
buggy code.

    $sum;        # Perl guru-ish.
    return $sum  # The way I really do it.  Old C habits die hard.

-- 
Michael R. Wolf
    All mammals learn by playing!
        [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to