Re: declaring with my

2004-09-14 Thread Gunnar Hjalmarsson
Errin Larsen wrote:
Hi all, straight out of the Learning Perl book (3rd edition, page
275) is this code:
my @numbers;
push @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
   printf "%20g\n", $_;
}
This works flawlessly.  My question is why can't I put that
variable declaration in the push function?  like this:
push my @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
   printf "%20g\n", $_;
}
Because the push() statement is in a loop, and my() would empty
the variable at each iteration.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: declaring with my

2004-09-14 Thread John W. Krahn
Errin Larsen wrote:
Hi all, straight out of the Learning Perl book (3rd edition, page 275)
is this code:
my @numbers;
push @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
   printf "%20g\n", $_;
}
This works flawlessly.  My question is why can't I put that variable
declaration in the push function?  like this:
push my @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
   printf "%20g\n", $_;
}
When I run this code, nothing is output.  It seems I see this kind of
variable-declaration-in-a-function all the time.  Why is it not
working here?
The original code could be written as:
my @numbers;
while ( <> ) {
push @numbers, split;
}
If you declare @numbers inside the while loop it will only be seen inside the 
loop.  What you want is this:

my @numbers = map split, <>;
Although that has to read the entire file first in order to process it while 
the while loop only reads one line at a time.


John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: declaring with my

2004-09-14 Thread Gunnar Hjalmarsson
John W. Krahn wrote:
Errin Larsen wrote:
my @numbers;
push @numbers, split while <>;

why can't I put that variable
declaration in the push function?  like this:
push my @numbers, split while <>;
The original code could be written as:
my @numbers;
while ( <> ) {
push @numbers, split;
}
Is that really identical to the original code as regards scoping?
If you declare @numbers inside the while loop it will only be seen 
inside the loop.
use warnings;
push my @numbers, split while <>;
my @numbers = (1,2,3);
generates the warning: ""my" variable @numbers masks earlier 
declaration in same scope at ...".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: declaring with my

2004-09-14 Thread Errin Larsen
> Because the push() statement is in a loop, and my() would empty
> the variable at each iteration.
> 
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl

Hi Gunnar ... Thanks for the help.

I assure the list, the following is the code EXACTLY as I was using it to test:
#!/usr/bin/perl

use warnings;
use strict;

my @numbers;
push @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
  printf "%20g\n", $_;
}

This works flawlessly, with no warnings or error messages.

I tried to modify the script, as seen below:

#!/usr/bin/perl

use warnings;
use strict;

push my @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
  printf "%20g\n", $_;
}

This code produces no output ... not even any warnings or errors.

Gunnar, I believe your explanation makes the most sense to me.  With
the my operator (function?) reseting the value of the @numbers
variable each time the loop is run, the output would not be in error,
it would just be empty.

So, now my question becomes, is there a way to watch this variable,
before, throughout and after the loop to confirm that it is being
reset each time.  More specifically, my problem now is, if the
variable is being reset throughout the loop, shouldn't the final
iteration of the loop have produced SOME output, albeit only the last
bits?  Maybe it has something to do with the precedence of the actual
line of code in the loop.  the:

  push my @numbers, split;

--Errin

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




Re: declaring with my

2004-09-14 Thread John W. Krahn
Gunnar Hjalmarsson wrote:
John W. Krahn wrote:
Errin Larsen wrote:
my @numbers;
push @numbers, split while <>;

why can't I put that variable
declaration in the push function?  like this:
push my @numbers, split while <>;
The original code could be written as:
my @numbers;
while ( <> ) {
push @numbers, split;
}
Is that really identical to the original code as regards scoping?
No it isn't.
perldoc perlsyn
[snip]
NOTE: The behaviour of a "my" statement modified with a statement modifier
conditional or loop construct (e.g. "my $x if ...") is undefined.  The
value of the "my" variable may be "undef", any previously assigned value,
or possibly anything else.  Don't rely on it.  Future versions of perl
might do something different from the version of perl you try it out on.
Here be dragons.
If you declare @numbers inside the while loop it will only be seen 
inside the loop.
use warnings;
push my @numbers, split while <>;
my @numbers = (1,2,3);
generates the warning: ""my" variable @numbers masks earlier declaration 
in same scope at ...".
Yes, the first declaration is in the same scope as the second declaration 
however, as it says in perlsyn, the value could be anything.

Since this is a beginners list I didn't want to get into all the gory details 
but this issue has been discussed in comp.lang.perl.misc and the perl5-porters 
mailing list if you want more details.  :-)


John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: declaring with my

2004-09-15 Thread Gunnar Hjalmarsson
Errin Larsen wrote:
Gunnar Hjalmarsson wrote:
Because the push() statement is in a loop, and my() would empty
the variable at each iteration.
push my @numbers, split while <>;
foreach (sort { $a <=> $b } @numbers) {
  printf "%20g\n", $_;
}
This code produces no output ... not even any warnings or errors.
Gunnar, I believe your explanation makes the most sense to me.  With
the my operator (function?) reseting the value of the @numbers
variable each time the loop is run, the output would not be in error,
it would just be empty.
So, now my question becomes, is there a way to watch this variable,
before, throughout and after the loop to confirm that it is being
reset each time.  More specifically, my problem now is, if the
variable is being reset throughout the loop, shouldn't the final
iteration of the loop have produced SOME output, albeit only the last
bits?
Yeah, I had the same thought. Suppose the fact that it doesn't contain 
the values from the last line proves that my theory is not the whole 
truth...  See John's explanations.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: declaring with my

2004-09-15 Thread Gunnar Hjalmarsson
Gunnar Hjalmarsson wrote:
Suppose the fact that it doesn't contain the values from the last
line proves that my theory is not the whole truth...
OTOH, you can study the impact of my() through these examples:
my @array;
for (1..3) {
push @array, $_;
print "@array\n";
}
Outputs:
1
1 2
1 2 3
for (1..3) {
push my (@array), $_;
print "@array\n";
}
Outputs:
1
2
3
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]