use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my %words; my $tot = 0; while () { for my $word (split) { $words{$word}++; $tot++; } } print Dumper(\%words); print "The number of words in the file is $tot\n"; __DATA__ a b c d e f g h a b c