hi monks I have a file with several hundred thousand numbers , and I need to determine the number of times each value is present in the file and print out a numeric sorted list of values and the number of times seen. I thought using a hash such a the example below would work . my idea was For each number in the array, if the number already exists in the hash, then increment the value by 1 ; else if it does not exist in the hash, then add the number to the hash with the initial value of 1 . HOWEVER IT'S NOT WORKING :( AND I CAN'T FIND WHY, PLEASE HELP
my @numbers = (1..100) my %count; foreach $number(@numbers) { if (exists $count{$number}) { $count{$number}++; } else { $count{$number} = 1; } } foreach (sort keys %count) { print "$number occurs $count{$number} time(s)\n"; }
In reply to counting elements using a hash by bk1388
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |