Since I'm having a complete mental block, I thought I could use the help of my fellow monks.
I have two arrays of variable length, I need to traverse both and search non matching values. ie.
Array 1
[1, 2, 4, 5, 6, 8, 15];
Array 2
[1, 3, 4, 5, 6, 7, 9];
What needs to happen in my case is that a loop needs to traverse array 1 and test against each value in array 2, if it doesn't match, then push the value I'm testing against into a different array, for clarity we'll call it @add. If a value matches, then nothing has to happen, just skip to the next iteration. So to complete the thought, the first set of results would contain the following (2,8,15) these results would get pushed into the @add array.
On the other hand, I need to step through array 2 and find all non-matching values, then push them into an array called @delete.
My immediate solution follows, though I find it significantly ugly: (untested)
my @delete = ();
my @add = ();
my $found = '';
for my $form_id (@tax_ids) {
$found = '';
for my $db_id (@db_state) {
if ($db_id != $form_id) {
next;
}
else { $found++; }
}
if (!$found) {
push @add, $form_id;
}
}
And do the exact loop again for the @db_state array, find all non-matching characters and push them into the @delete array.
What's a better solution? Quicker, smoother, less ugly? :)
Thanks!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.