Hi Bilbo nice work. Particle made a couple of points that I agree with. Using modifiers when appropriate IMO produces more intuitive and straight forward code. However in this case I would say that this is a minor improvement to a non-optimal solution. (People no lectures on premature optimization please, I've heard them all before and I'm not interested in debating if this is 'premature' or not.)

Keeping track fo the frequencies, and then sorting them and using only one element is wasteful. A more efficient or scalable approach would be to simply add an if to the inner loop that keeps track of the mode key and mode count for the part of list read so far. Once completeing the list this value is the correct one.

use strict; # Set up a hash, where $freq{word} = no of occurences of 'word' # (where word is actually a number in this case) my %freq; # Read from filename given on command line, or stdin if no file # name is given my ($mode_count,$mode_key)=(0,undef); while (<>) { chomp; # lose newlines from the lines # Split the line by whitespace and iterate over the results foreach (split (/\s+/, $_)) { if (++$freq{$_}>$mode_count) { # increment our frequency count +er # And keep track of the most common element $mode_count=$freq{$_}; $mode_key=$_; } } } print "The mode is $mode_key with $mode_count hits\n";
Incidentally for the record I havent read the thread this is in. I only read your node because you linked to it in another node... If I'm repeating something then apologies.

UPDATE: Sigh. I really should have read the thread first. Now I see why you were building a list and then sorting it. Apologies.

Hmm, on rereading I suppose its possible that if there were very few types of item that my apporach would actually be slower than yours (I'd have to benchmark to be sure) But i think that in the average case the sort is overkill.

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.


In reply to Re: Re: calculating the mode by demerphq
in thread calculating the mode by Anonymous Monk

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.