Re: Newbie question about variables, arrays and where they all go

2009-01-19 Thread Telemachus
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: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie question about variables, arrays and where they all go

2009-01-19 Thread dolphin_sonar
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/




Re: Newbie question about variables, arrays and where they all go

2009-01-18 Thread Telemachus
From: Telemachus 
Date: Sun, 18 Jan 2009 20:17:27 -0500
To: beginners@perl.org
Subject: Re: Newbie question about variables, arrays and where they all go

On Sun Jan 18 2009 @  7:54, Telemachus wrote:
> The arguments to a subroutine go into the @_ array. The @numbers array is
> empty until you load it up in the foreach loop. 

Edit: I should have said that the @numbers array is empty before the *first*
run. Since it's persistent, after that it already contains all the items from
previous calls. So on a second call, it already has 5 and 6. (I should also
consider spelling 'understand' with a d.)

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




Re: Newbie question about variables, arrays and where they all go

2009-01-18 Thread Telemachus
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 understaning 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

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