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
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
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 t
> 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:
#!/u
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 @
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
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";
}
Ou