Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: calculating the mode

by frankus (Priest)
on Jun 10, 2002 at 09:37 UTC ( #173060=note: print w/ replies, xml ) Need Help??


in reply to calculating the mode

Why are there so many lines here? AFAIKS all you need is a hash, a sort and an array.

$counts{$_}++ for @array_of_numbers; my @sorted_array = sort { $counts{$a} <=> $counts{$b} } keys %counts;

Basically, go through the array and tally the occurances of numbers within a hash.
Finally sort the keys of the hash using a sort on the number or occurances.

Or have I missed something?

Amended: thanks to Molt ;)

--

Brother Frankus.

¤


Comment on Re: calculating the mode
Download Code
Re: Re: calculating the mode
by Anonymous Monk on Jun 10, 2002 at 10:23 UTC
    Thanks frankus, however i realise that i am ignorant and incompetent but i dont know how to go through the array and tally the occurances of numbers within a hash! :-) I also dont know how to sort the keys of the hash using a sort on the number of occurances. THANKS FOR BEING PATIENT!!! slow perl_learner!!
      I hope I did not make you feel ignorant or incompetent, was my answer too terse?
      I am working at the same time as posting here.

      Your solution seems to be the solution of one used to another language.
      I've made no judgement about your abilities that I can see.
      I am sorry if you feel they did. Go easy on the uppercase ;o)

      --

      Brother Frankus.

      ¤

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-10-26 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (153 votes), past polls