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.
- 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.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; } } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |