castaway has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing array contents to DB contents
by bart (Canon) on Jan 30, 2003 at 10:37 UTC | |
by castaway (Parson) on Jan 30, 2003 at 11:50 UTC | |
|
Re: Comparing array contents to DB contents
by Gilimanjaro (Hermit) on Jan 30, 2003 at 10:31 UTC | |
by Gilimanjaro (Hermit) on Jan 30, 2003 at 10:38 UTC | |
|
Re: Comparing array contents to DB contents
by Gilimanjaro (Hermit) on Jan 30, 2003 at 13:19 UTC | |
|
Re: Comparing array contents to DB contents
by CountZero (Bishop) on Jan 30, 2003 at 12:00 UTC | |
by castaway (Parson) on Jan 30, 2003 at 12:04 UTC | |
by CountZero (Bishop) on Jan 30, 2003 at 15:58 UTC | |
|
Re: Comparing array contents to DB contents
by castaway (Parson) on Jan 30, 2003 at 11:17 UTC |