in reply to Counting the frequency of words in a string
=head2 How can I print out a word-frequency or line-frequency summary? To do this, you have to parse out each word in the input stream. We'l +l pretend that by word you mean chunk of alphabetics, hyphens, or apostrophes, rather than the non-whitespace chunk idea of a word given in the previous question: while (<>) { while ( /(\b[^\W_\d][\w'-]+\b)/g ) { # misses "`sheep'" $seen{$1}++; } } while ( ($word, $count) = each %seen ) { print "$count $word\n"; } If you wanted to do the same thing for lines, you wouldn't need a regular expression: while (<>) { $seen{$_}++; } while ( ($line, $count) = each %seen ) { print "$count $line"; } If you want these output in a sorted order, see L<perlfaq4>: ``How do +I sort a hash (optionally by value instead of key)?''.
|
|---|