On Jan 18, 7:54 pm, telemac...@arpinum.org (Telemachus) wrote:
> On Sun Jan 18 2009 @ 10:59, dolphin_sonar wrote:
>
> >    1 # When calling 'running_sum(5, 6);' the variable 'state @numbers'
> > receives those two
> >    2 # parameters, (5 and 6), right? Then, the @numbers array also
> > copies/stores (5 and 6)
> >    3 # into the special '( @_ )' variable as well, right? Also, line
> > 13 pushes '$number' into
> >    4 # the '@numbers' array each time through the foreach loop,
> > right?
>
> Maybe I'm not understanding what you mean, but I think you're confused. The
> arguments to a subroutine go into the @_ array. The @numbers array is
> empty until you load it up in the foreach loop. Having 5 and 6 as arguments
> does not automatically put those items into @numbers, nor does @numbers
> copy anything into @_.
>
> Also, I understand that you may just be testing out persistent variables in
> 5.10, but this program is confusing.
>
> Consider what happens if you call running sum a second time (say as
> running_sum( 2, 7 );) If I add that call, here's my output:
>
>     telemachus ~ $ perl sum
>     The sum of (5 6) is 11
>     The sum of (5 6 2 7) is 31
>
> That appears to say that perl has added 5, 6, 2, and 7 up to 31. What
> actually happened was that you added 5, 6, 2 and 7 (20) to 11 (the sum from
> the previous call to running_sum - which was saved). To put this another way
> around: maybe you want to keep a running sum, but the print statement in the
> subroutine is very confusing.
>
> Hope this helps, T

T!

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.

Regardless, I now understand exactly what your saying about the state
@numbers declaration. I was confused about that and that helps me
understand a lot of what is going on, that was important for me to
understand. The example code actually had two more calls to running
sum: running_sum( 1..3 ); and running_sum( 4 ); (see the link to the
code and the out below).

Here is a link to the example code in a much better format, you can
see the code better than using Gmail IMO. http://sial.org/pbot/34575

# The output I get each time is this:
The sum of (5 6) is 11
The sum of (5 6 1 2 3 ) is 17
The sum of (5 6 1 2 3 4 ) is 21

This example comes from page 68 O'Reilly Learning Perl 5th edition.
They also used 'say' for the first time in the book instead of print
as well, which was kind of confusing to me.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to