You're kind of repeating yourself inside and outside your while loop, so that can be condensed. Here's a more perlish way to do it. It confines the $word variable to the smallest possible scope, and uses a statement modifier after last to shorten things and make the logic clearer (in my opinion).
while( my $word = <STDIN> ){
chomp $word;
last if $word eq 'done';
$counter{$word}++;
}
On your foreach loop, you can eliminate the if/else with a ternary operator:
for my $word (keys %counter){
print "You typed $word $counter{$word} time" .($counter{$word}>1 ? '
+s' : ''). ".\n";
# or
printf "You typed %s %d time%s.\n", $word, $counter{$word}, ($counte
+r{$word}>1?'s':'');
}
Aaron B.
Available for small or large Perl jobs; see my home node.
|