Let's answer your last question first: How to print the line "ab aabb aaabbb" and not "xxyy xxxyyy": Since your key is the unique list of letters found in each word, your signatures are "ab" and "xy". You want to print the word list only if it contains a word that contains all the letters, but not any repeats.
As you guess, this is a simple condition. You already know that all words contain all the letters in the list, so that condition is met automatically. Since you want a word that contains only those letters in your signature with no repeated letters, all you need to do is check the length: If any word in the list is the same length as the key, both conditions are met, and you can print the list, by adding two lines to your print loop, like so:
... for (sort keys %words) { my $siglen = length; next unless grep { $siglen == length } @{$words{$_}}; ...
(How does Perl know this? Because “push” expects an array? Because the @ symbol lets Perl know?)
Perl has a built-in feature called "autovivification" where if you add something to a data structure that doesn't exist, it tries to build it for you. I don't know exactly where/how it happens, but my mental model of the push @{$words{$signature}}, $_; statement goes something like this:
. . . # push @{$words{$signature}, $_; $rTemp = $words{$signature} call push_op($rTemp, $_); . . . push_op: $rArray = get_array_ref($arg[0]); $rArray[$#rArray+1] = $_; return; get_array_ref: if (!defined $arg[0]) { return [ ]; } if ("ARRAY" ne ref $arg[0]) { die "Not an ARRAY reference"; } return $arg[0]; }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: Anagrams & Letter Banks
by roboticus
in thread Anagrams & Letter Banks
by dominick_t
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |