You can also substitute $sth4->fetch() for $sth4->fetchrow() and get rid of @row4:
If i understand your code, you have a list of site numbers in @array that may or may not match results from the database. If the first item of the sites array matches the first row of the database result, you want to modify it. Otherwise, you want to delete.while ($sth->fetch()) { # do comparison }
Where I'm confused is the relationship of the array and the db results. If there's an off-by-one in either, the rest of the results will be skewed.
It seems to me to make more sense to test for the existence of database results in the sites array, which could be done in one of two ways.
Keeping your code mostly similar, we build a hash instead of an array:
The other option would be to let your database do the work, splitting and joining $sites to form a VALUES IN ( ) clause for your SQL statement. Depending on how familiar you are with SQL and Perl, you may prefer one approach over the other.my %sites_list; @sites_list{split(" ", $sites)} = (); # hash slice for quicker +lookup while ($sth4->fetch()) { if (exists $sites_list{$newSiteNumber}) { print "modify!"; } else { print "delete!"; } }
Or I could be completely misunderstanding your intent here, in which case you get double your money back.
In reply to Re: Parsing Arrays II
by chromatic
in thread Parsing Arrays II
by jasmine
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |