next if $seen{$key}++; push @{$words{$signature}}, $_;

You've already received some excellent answers, I just wanted to touch on your questions about these two lines of code. Both of them involve the concept of autovivification, where elements of data structures are brought into existence simply by referring to them.

The postincrement operator ++ first returns the old value of its argument and then increments it (the argument $seen{$key}, not the returned value). This means that if the key does not yet exist in the hash, it will return the undefined value, which in the boolean (scalar) context of if evaluates to false, then brings the hash entry into existence, with a value of 1 (Update 2: because undef in numeric context is 0, so 0+1=1, and in this special case there is no warning about using undef in a numeric operation). The next time this key is encountered, that means that the operator returns the previous value of 1, which is true, so the next is executed, thereby skipping duplicates. Since the increment is also executed, this also means that the values of the hash count how many times each key is seen.

In the second case, the array dereference operation @{} tells Perl explicitly that you expect that hash element to contain an array reference, so if the hash element does not yet exist, it will be brought into existence as an empty anonymous array, so then it can be pushed onto.

As for your questions about for and $_, I find that the first few paragraphs of Foreach Loops explains this fairly well.

Update: I strongly recommend you Use strict and warnings, also have a look at the Basic debugging checklist.

Update 2019-08-17: Updated the link to "Truth and Falsehood".


In reply to Re: Anagrams & Letter Banks by haukex
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.