Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: optimize percentile counting in hash

by hipowls (Curate)
on Mar 20, 2008 at 23:48 UTC ( [id://675343]=note: print w/replies, xml ) Need Help??


in reply to optimize percentile counting in hash

You need to wrap your code in <code> and </code> tags. The characters [ and ] are special to perl monks and can't put put as literal text in your post. You should also use <p> and </p> tags around paragraphs.

A general tip is to put

use strict; use warnings;
at the start of every script you write until you know what you are doing. They wil pick up typos and dubious practices.

Assuming I've understood your problem then this does what you want.

my @data = get_data(); # or however you get them my %count; foreach my $datum (@data) { ++$count{$datum}; }; foreach my $item_count ( values %count ) { $item_count *= 100/@data; }
Now %count will have a key/value for each datum and its frequency as a percentage.

Replies are listed 'Best First'.
Re^2: optimize percentile counting in hash
by max210 (Novice) on Mar 21, 2008 at 01:36 UTC
    Thanks for trying to help me out. I really appreciate it :)

    I was not using \<code\></code> may be that's y u had error. The code should work now. I HAVE TO FIND PERCENTILE AND NOT PERCENTAGE :( which makes it more difficult to execute in less time.

    Eagerly waiting for reply, thanks again
      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; }

        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

      For future reference, if you modify your original post, you should also add a Revised: note, explaining the change. Otherwise it's confusing to the people new to the thread.
        sorry, got logged out and posted msg as anonym.... the formula for percentile is given by me, sorry for confusion...... plz help
        will keep that in mind, thanks :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://675343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found