--- "Helio S. Junior" <[EMAIL PROTECTED]> wrote:
> Hello,

Hi =o)

> How do i read a simple file and look for "repeated
> words" in each line, writing out the words i have
> found and the numbers of line they were found?
> 
> eg:
> File ==> Test.Dat
> sample line of text.
> this line follows another line.
> This is the last line.
> 
> The program should report:
> 
> Repeated Word(s): 'line'  on Line 2.

I was tempted to use something like 
 $line =~ /(\b\w+\b).*\1/o;

to find repeats, but don't -- it only reads the first repeated word,
and gets more complex to fix after that.

Instead, try doing it manually:

====================================
open DAT, "Test.dat" or die $!;
my $ln = 0;
foreach my $line (<DAT>) {
   chomp $line;
   $ln++;
   my %hit = ();
   foreach my $word (split /\W+/o, $line) { $hit{$word}++ }
   foreach my $word (keys %hit) {
      print "Repeated Word(s): '$word'  on Line $ln.\n"
          if $hit{$word} > 1;
   }
}
close DAT;

=====Test.dat====================
sample line of text.
this line follows another line.
This is the last line.
and foo and foo and foo.
=================================

You could even use this to tell you how many times the word appeared on
the line by adding $jit{$word} to the printed line, etc.

This could be condensed, but it works.

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to