There are far better and shorter ways than this one, but I'm hoping this will allow you to see what's happening. If I understand you correctly, you want to extract the number in the input that has the highest 'count'. If so:
#!/usr/bin/perl use strict; use warnings; my @data = qw(2 3 3 3 5 7 8 12 32 44 55 12 3 23 43 33 1 4 25 43 42 1 4 + 5 3 3 3); my %seen; for my $num (@data){ $seen{$num}++; } my @keys = sort {$seen{$b} <=> $seen{$a}} keys %seen; my $highest = $keys[0]; my $count = $seen{$keys[0]}; print "Highest count: $highest, Count: $count\n";
It defines an idiomatic %seen hash for keeping track, and using each value in the data, sets the key as the number in data, and adds one to the current value. Then, we create a keys array with a backwards sort which populates the array with the data number that has the highest value (count) to lowest. We then set the count value to $count and the key (data number) to $highest.
If I misunderstood what you need, just clarify.
-stevieb
In reply to Re: Help fixing this piece of code
by stevieb
in thread Help fixing this piece of code
by perlynewby
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |