btw - yes, we have already covered that I should be using strict; and declaring my variables, that I should really be slurping my text file (although the script currently does what it should thru some kludgy perl magic ;), and a few other things I need to fix before I turn it in. :-)
The fundamental change that you need to make to become a good Perl prgrammer (and a good programmer in general), is that you have to use strict
before you code, and declare your variables
as you write them, not at the end.
You are driving blind. When things don't work, you don't know why. With use strict, 90% of all your errors will be caught by the compiler. Why make things harder on yourself?
As for the problem at hand, you don't need two loops, you need one, but you need to exit the loop when you have gone through it 10 times. Try:
my $ctr=1;
foreach $key (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
print "$key\t\t= $hash{$key}\n";
last if $ctr++ == 10;
}
last tells the interpreter to exit the loop unconditionally.
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."