> 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
while(<STDIN>) will create an infinite loop, so you do need some sort of sentinel value to break out of the loop if you encounter it in the user input. For example, you could do: while(<STDIN>) { last if /^\-1/; print } Which terminates the loop when the user enters -1 at the beginning of a line. Or you could read from a file which would make it easier to test for the end of input. Hope that helps, -dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]