Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added find_duplicate_words.

  • Loading branch information...
commit d0804e97cf861723ecdf6cb0abf8300dc2632abf 1 parent f0fb8de
@jgm authored
Showing with 55 additions and 0 deletions.
  1. +55 −0 find_duplicate_words
View
55 find_duplicate_words
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# Finds duplicate adjacent words.
+
+use strict ;
+
+my $DupCount = 0 ;
+
+if (!@ARGV) {
+ print "usage: dups <file> ...\n" ;
+ exit ;
+}
+
+while (1) {
+ my $FileName = shift @ARGV ;
+
+ # Exit code = number of duplicates found.
+ exit $DupCount if (!$FileName) ;
+
+ open FILE, $FileName or die $!;
+
+ my $LastWord = "" ;
+ my $LineNum = 0 ;
+
+ while (<FILE>) {
+ chomp ;
+
+ $LineNum ++ ;
+
+ my @words = split (/(\W+)/) ;
+
+ foreach my $word (@words) {
+ # Skip spaces:
+ next if $word =~ /^\s*$/ ;
+
+ # Skip punctuation:
+ if ($word =~ /^\W+$/) {
+ $LastWord = "" ;
+ next ;
+ }
+
+ # Found a dup?
+ if ($word eq $LastWord) {
+ print "$FileName:$LineNum $word\n" ;
+ $DupCount ++ ;
+ }
+
+ # Mark this as the last word:
+ $LastWord = $word ;
+ }
+ }
+
+ close FILE ;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.