#!/usr/local/bin/perl my $dups = &has_dup_letters (@ARGV); if ($dups) { print "There is/are $dups dup(s).\n"; } else { print "There are no dups.\n"; } sub has_dup_letters { # Takes a word and optionally an alphabet. # Returns the number of duplications (0 for none). eg abc # Returns the number of duplications are measured as letters removed to # make it unique. eg abbc == 1; abbbc == abbcc ==2; # Characters not in the alphabet are ignored regardless of # duplication. # If you do not supply an alphabet it will use a-z. If you wanted # to check different characters (vowels) or numbers or punctuation, # just include them in your alphabet. # Case of A-Z is ignored. Trailing \n of words are chomped. my ($word, $alphabet) = @_; unless ($alphabet) { $alphabet = 'abcdefghijklmnopqrstuvwxyz' }; $alphabet_size = length $alphabet; chomp $word; # down case it $word =~ tr/A-Z/a-z/; # Remove chars not in the alphabet. $word =~ s/[^$alphabet]//g; $alphabet =~ s/[$word]//g; unless ((length($alphabet) + length($word)) == $alphabet_size) { length ($alphabet) + length ($word) - $alphabet_size; # has dups } else { 0; # No Dups } }