develooper Front page | perl.beginners | Postings from March 2002

Printing frequency of word occurence in paragraph

Thread Next
Bruce Ambraal
March 22, 2002 00:13
Printing frequency of word occurence in paragraph
Message ID:
In the program below I don't know when to stop the loop.
 (CTRL + d ) will just terminate the program, leaving the data via STDIN unattended, this is my problem.

Lets define what I want to do:
I want to print the frequency of every word at the beginning. and every word at the end of a line.

This could mean two things:
1) one line of text to be read in and on pressing <enter> code below
will analyse the input.
2) a paragraph containing a number of lines get adressed.

I think my code adress 2) but problem with this is that I don't know when my coding should jump out of the loop?

Any ideas

> #!/usr/bin/perl
> while (<STDIN>) {
>   if (/(\b[^\W_\d][\w-]+\b)/g) {
>     $gotit{$1}++;
>   }
>   if (/(\b[^\W_\d][\w-]+\b\Z)/g) {
>     $found{$1}++;
>   }
> }
> while (($wrd,$cnt)= each %gotit){
>         print "$cnt $wrd\n";
> }#end of while
> foreach my $yes (keys (%found)){
>         print "$found{$yes} $yes\n";
> }#end foreach

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About