Hey perl experts, I am trying to create a perl script on windows active perl, that prints a word only once for a group of adjacent repeats, and that prints the line number that these words appeared. I've created a script that shows just the words are adjacently repeated with their line numbers but it shows all the repeats of a particular group rather than just one of them. Here is the script that I have done. I'd really appreciate your help, as I am a new learner of perl. Thank you.
#!/usr/bin/perl # rcwords.pl: print immediately adjacent repeated words once from inpu +t, # even if they are repeated more than once, # and print out these words along with the line number(s) that they ap +peared. use English; use diagnostics; $prevword = ''; $n = 0; while ($line = <>) { $n = $n + 1; $line =~ s/[[:punct:]]/ /g; $line = lc $line; @words = split /[[:space:]]+/, $line; foreach $word (@words) { if ($word ne '') { if ($word eq $prevword) { print " $n $word\n"; } $prevword = $word; } } }
20050301 Janitored by Corion: Fixed formatting
In reply to removing repeats by dummy2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |