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 counter # And keep track of the most common element $mode_count=$freq{$_}; $mode_key=$_; } } } print "The mode is $mode_key with $mode_count hits\n";