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; }

In reply to optimize percentile counting in hash by max210

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.