use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my %words; while () { $words{$_}++ for split; } print Dumper(\%words); __DATA__ the the the and me ok big dog me #### $VAR1 = { 'and' => 1, 'big' => 1, 'dog' => 1, 'me' => 2, 'ok' => 1, 'the' => 3 };