I have the need to compare 2 arrays. If an element in array exists in array 1 and array 2, remove it from array 1. Right now, I'm doing this by a standard nested loop method, but this is taking way to long. What I'm looking for is a speedy and concise method to do the following:
WORDS: for($x=0; $x<@words; $x++) { foreach(@exclude) { if (($words[$x] =~ m/\W) || # remove non-words ((length($words[$x])) < 4) || # ignore words less than +4 characters (($x > 0) && (lc($words[$x]) eq lc($words[$x-1]))) || # + drop duplicates (lc($words[$x]) eq lc($_)) ) { splice (@words, $x, 1); $x--; next WORDS; } } }
I should note that @words is sorted before this loop begins. That doesn't seem to be creating any slowness in the code, as @words tends to be small in comparison.

Right now, it can take 2-3 seconds each time this loop is running. There as GOT to be a better way to do this than what I have. It works, but not well enough. Must be faster.


In reply to Array Comparison by mcogan1966

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.