mcogan1966 has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.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; } } }
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Array Comparison
by chromatic (Archbishop) on Dec 22, 2003 at 18:32 UTC | |
by Roy Johnson (Monsignor) on Dec 22, 2003 at 18:37 UTC | |
by mcogan1966 (Monk) on Dec 22, 2003 at 18:54 UTC | |
by Roger (Parson) on Dec 22, 2003 at 23:12 UTC | |
Re: Array Comparison
by Ovid (Cardinal) on Dec 22, 2003 at 18:37 UTC | |
Re: Array Comparison
by jZed (Prior) on Dec 22, 2003 at 18:41 UTC | |
Re: Array Comparison
by Roy Johnson (Monsignor) on Dec 22, 2003 at 19:00 UTC | |
by mcogan1966 (Monk) on Dec 22, 2003 at 19:16 UTC | |
Re: Array Comparison
by Not_a_Number (Prior) on Dec 22, 2003 at 20:11 UTC | |
by mcogan1966 (Monk) on Dec 23, 2003 at 20:09 UTC | |
by Not_a_Number (Prior) on Dec 23, 2003 at 20:31 UTC | |
Re: Array Comparison
by duff (Parson) on Dec 22, 2003 at 18:43 UTC | |
by mcogan1966 (Monk) on Dec 22, 2003 at 18:58 UTC | |
Re: Array Comparison
by thraxil (Prior) on Dec 22, 2003 at 23:15 UTC | |
by mcogan1966 (Monk) on Dec 23, 2003 at 20:07 UTC |
Back to
Seekers of Perl Wisdom