> Given a list whose elements are strings (containing only letters, as they are actually English-language words), how would one print that list only on the condition that at least one of the strings in the list contains no repeated letters?
Counting repetitions or not repetitions is a call for a hash. The following iterates over lists and, using the $has_unique switch, search if one word of the list is built with just different characters.
This is done with the %chars hash: $chars{$_}++ for ($word =~ /./g) where the regex in list context returns all chars and the respective value in the hash is augmented by one.
Then if the length of the $word is equal to the number of keys of the hash the word must be built with different characters, so the switch $has_unique is turned on and after all word in the list were processed the list is printed only if the switch is on.
use strict; use warnings; my $good = [qw( allo mallo malo)]; my $bad = [qw( tillo sillo sallo)]; foreach my $list ($good,$bad){ my $has_unique; foreach my $word(@$list){ my %chars; $chars{$_}++ for ($word =~ /./g); if (scalar keys %chars == length $word){ # print "$word has no repeated letters"; $has_unique++; } } print +(join ' ', @$list),"\n" if $has_unique; } # output allo mallo malo
L*
In reply to Re: Anagrams & Letter Banks
by Discipulus
in thread Anagrams & Letter Banks
by dominick_t
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |