Hello monks. I am a biologist trying to add Perl scripting to my skill set. I've been working through the "Unix and Perl Primer for Biologists", which has been a lot of fun http://korflab.ucdavis.edu/unix_and_Perl/.
I've been having so much fun with it that I have been screwing around with non biological problems, including writing a multi word anagram solver. I realize that many people have done this before, but struggling through making my own has taught me quite a bit that I didn't glean from the course.
Anyway, I've run in to a bit of snag when trying to subtract a found anagram from the larger letter list from which it came. I'd like to be able to take the leftover letters and search for more anagrams within them.
I did a lot of searching, and was initially trying to do this by converting the found anagram to a hash, and then asking if each character in the letter list existed in the hash. The problem with this approach is that if a particular word has multiple occurrences of the same letter (like "butt"), only one of the letters can be loaded in to the hash because the keys have to be unique ("butt" becomes "but").
I've since come up with this, which sort of works, as long as I don't use Warnings (I realize this is bad!):
#!/usr/bin/perl use strict; my @letters = qw(a a a b b b c c c); my @word = qw(a a b b); my $length = @letters; my $wlength = @word; for (my $i = 0; $i < $length; $i++) { for (my $j = 0; $j < $wlength; $j++) { if (($letters[$i]) eq ($word[$j])) { splice (@letters, $i, 1); splice (@word, $j, 1); } } } print "@letters\n";
This works as long as the "word" contains fewer copies of a given character than the "letters", but will not remove the final copy of a character if there are the same number of occurrences in both. For example, using the above code to subtract "a a b b" from "a a a b b b c c c" gives "a b c c c", however, when I subtract "a a a b b" from the same set of letters, I still get "a b c c c" instead of the desired "b c c c".
I am thinking that there is a better way to approach this problem, but I can't for the life of me find an easier way to do it. Thanks in advance for any guidance you can provide!!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |