in reply to Re^2: optimize percentile counting in hash
in thread optimize percentile counting in hash

How about
use strict; use warnings; my @data = get_data(); # or however you get them my %count; foreach my $datum (@data) { ++$count{$datum}; } my %percentile; my $total = 0; foreach my $datum (sort { $a <=> $b } keys %count) { $total += $count{$datum}; $percentile{$datum} = $total / @data; }

Replies are listed 'Best First'.
Re^4: optimize percentile counting in hash
by Anonymous Monk on Mar 21, 2008 at 02:27 UTC

    its giving incorrect percentile values :( to find percentile rank of score x out of n scores:
    (number of scores below x)/n*100 = percentile rank
    need to sort the hash first, i used index value of the array to find number of scores below x. x is value of hash taken one by one and returning the percentile

      oh, below, not below or equal. Just switch the order of the two statements in the last loop. I usually only do the *100 when it's time to print, but I added it for you.
      use strict; use warnings; my @data = get_data(); # or however you get them my %count; foreach my $datum (@data) { ++$count{$datum}; } my %percentile; my $total = 0; foreach my $datum (sort { $a <=> $b } keys %count) { $percentile{$datum} = $total / @data * 100; $total += $count{$datum}; }