max210 has asked for the wisdom of the Perl Monks concerning the following question:
@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; }
|
|---|