Hi,
I am trying to count percentile of million of hash value. I had written a subroutine for that which returns hash with values as percentile but its taking like infinite time. Kindly help me optimize the code or plz give different logic.
I learned perl just before couple of days :)
@random_values=0;
$abc = 0;
$ct=0;
@coins = ();
%hash = ("Quarter", 25, "Dime", 10, "Nickle", 5);
%returned_hash = hashPercentile(%hash);
print %returned_hash;
sub hashPercentile{
$array_length = @_;
for($i=0;$i<$array_length;$i++){
$j = $i+1;
$hash {$_[$i]} = $_[$j];
$i++;
}
@keys = sort {
$hash{$a} cmp $hash{$b}
} keys %hash; # and by value
while ( my ($key, $value) = each(%hash) ) {
$random_values[$abc] = $value;
$abc++;
}
@sorted = sort { $a <=> $b } @random_values;
foreach(@keys){
$value = $hash{$_};
my $search_for = $value;
my( $index )= grep $sorted[$_] eq $sea
+rch_for, 0..$#sorted;
#$index = No of elements below $searc
+h_for
# $n = No of elements
$n= scalar(@sorted);
$PR = ($index*100)/$n;
$account_ID_PR {$_} = $PR;
}
return %account_ID_PR;
}