Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Anagrams & Letter Banks

by Laurent_R (Canon)
on Oct 27, 2017 at 19:02 UTC ( [id://1202182]=note: print w/replies, xml ) Need Help??


in reply to Anagrams & Letter Banks

The %seen hash is there to remove duplicate words from the input. If a word in $key has nos been seen, then $seen{$key} will not exist and return a false value, so that we will continue the process; at the same time, the hash entry will be created for the key and the value will be 1. If the word has been seen, then $seen{$key} will exist and return a true value (1, the first time you see a duplicate word, then 2, 3...), we will go back to the top of the loop starting with the next input word.

The %word hash will have normalized words (i.e. with letters put in alphabetic orders) or signatures as keys, and for each such key, will contain an anonymous array of the original words that were translated into this signature. In other words, if an array in the hash or arrays has more than one entry, then it means that two or more words matched the normalized signature and that we have an anagram.

I hope this makes it clearer.

Replies are listed 'Best First'.
Re^2: Anagrams & Letter Banks
by dominick_t (Acolyte) on Oct 27, 2017 at 19:40 UTC
    Thank you, Laurent_R. That definitely clears up how the %seen hash is doing its thing. The bit about the %word hash I already understood . . . It was just the use of $_ that threw me for a moment. If you have thoughts on the question asked at the beginning of the (edited) post, I'd appreciate that as well! Thanks very much for you've already written here.
      Basically, you have two loops in a row. In the first loop, each record is assigned to $_. When this loop is finished, $_ does not have any useful value. When you start the second loop, again, each value is assigned to $_.

      No problem here, but there are cases, such as nested loops, where is might become slightly dangerous (you might clobber the value of the outer $_ with the value of the inner $_); in the cases of nested loops, while loops and for don't do exactly the same thing, so it is somewhat unreliable and don't do it unless you really know what you're doing. The best, in the event of nested loops, is to use explicit variables rather than the default topical variable $_.

      On the questions related to your other project (not anagrams), you don't give enough details on what you need and how it is supposed to work. Please specify.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-29 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found