#! /usr/local/bin/perl -w 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 while (<>) { my @array = split (/\s+/, $_); # Update the hash foreach (@array) {$freq{$_}++} } # Sort the keys of the hash (the words or numbers in the file) into # an array in ascending order of $freq{key} (the number of occurences) my @sorted_array = sort { $freq{$a} <=> $freq{$b} } keys %freq; # The mode is the last value in the array my $mode = pop(@sorted_array); print "The mode is $mode\n";