Hi all,
Not exactly sure what to call this one, its actually about comparing two arrays, or better, calling a sub and passing it pairs of values (one from each array), and only doing something if all the answers are the same. Hmm, thats not a good explanation either ;)

I'm maintaining some tables in a database that contain an ID field, and a varying number of other fields. I'm trying to write a sub which takes values for each of the fields as arguments compares them with whats already in the table, and either returns an ID (because they are already there) or inserts them and then returns the new ID.
So, I've got the contents of the table in an array of arrays (rows, then fields) and want to compare these using String::Approx, and only when all fields are similar to the array values, should it return the ID.

The rows-of-fields are in '$list', and the values in @vals:
if(@{$list}) { foreach $row (@{$list}) { my $Found = 1; for(my $i = 0; $i < @vals; $i++) { if($vals[$i] && $row->[$i+1] && String::Approx::amatch($row->[$i+1], $vals[$i])) { print "Found: " . $row->[0] . "\n"; $Found = $row->[0]; } else { $Found = 0; } } if($Found) { return $Found; } } }
- Doesnt work, as $Found has a value even when only one field/value pair matches, and I only need an ID when all fields in a row match.
jdporter suggested something like this, but I'd neglected to mention that I need the end result:
my $result = 1; for my $i ( 0 .. $#vals ) { $result &&= foo( $vals[$i], $arr[$i] ); } if ( $result ) { ..

(This started out as a sub which assumed there were exactly two values to compare, and just called String::Approx::amatch twice for each row, and somehow I can't get it to be generic.. Theres probably a simple solution.. )

C.


In reply to Comparing array contents to DB contents by castaway

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.