On Mon Jan 19 2009 @ 4:21, dolphin_sonar wrote:
> Again, you answered my question. You should be teaching Perl as your
> responses are very clear and that's not easy for people to do...give
> clear, concise responses.
Actually, I had a big goof in my response. The program and the print
statement are fine as you had them, and *I* got myself turned around.
When I typed up your program, I was thinking of other ways I might do it,
and I produced this:
sub running_sum {
state $sum = 0;
state @numbers;
push @numbers, @_;
$sum += $_ foreach @numbers; ## WRONG - should be foreach @_
say "The sum of (@numbers) is $sum";
}
I thought it would be easier to push the whole @_ array into @numbers at
once and add everything into $sum in a one-liner as well. But I did it
wrong.
The problem was that I added the entire @numbers array to $sum, but from
from call to call, I only want to add the new items (the ones in the @_
array). That's what I get for trying to answer a question and rewrite the
example all at the same time.
As for 'say' it's a new feature in 5.10 (like the state variables) which
allows you to print without explicitly asking for the "\n" - it looks like
it's officially introduced in Learning Perl 5e on page 90.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/