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] <http://learn.perl.org/> <http://learn.perl.org/first-response>