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. |