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!!


In reply to Subtracting one array from another (both have duplicate entries) by dougbot

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.