If you want to check for dups that are broader (letters, numbers punctuation) or narrower (eg just vowels) than just normal letters you can supply your own alphabet.
For illustration purposes the function is dropped in to a trivial program.
#!/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 remov +ed 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 du +ps } else { 0; # No Dups } }
In reply to Find duplicate chars in a string by monkfish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |