dominick_t:

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.