On Fri, 21 Jan 2005 18:03:29 -0500, Jay <[EMAIL PROTECTED]> wrote: > On Fri, 21 Jan 2005 17:56:17 -0500, Jay <[EMAIL PROTECTED]> wrote: > > On Fri, 21 Jan 2005 19:20:44 +0000, Mark Martin <[EMAIL PROTECTED]> wrote: > > > Hi , > > > I'm getting confused as to whether I need a last, next, redo or all of the > > > above : > > > > > > foreach $file_item ( @file_items ) > > > { > > > ($file_item_code,$file_item_description) = split /,/,$file_item ; > > > $count ++; > > > > > > $sth->execute($file_item_code); > > > > > > while ( @fetch = $sth->fetchrow ) > > > { > > > $database_item_description = $fetch[0]; > > > > > > if ( $file_item_description ne $database_item_description ) > > > { > > > splice ( @file_items, $count, 1 ); > > > } > > > > > > } > > > > > > } > > > > > > Something will be done with each element of @file_items but only if each > > > elements description matches the corresponding database elements > > > description. > > > If there is no match then it should be removed from the array. > > > > > > This is working but not for adjacent items whose descriptions don't match > > > their database counterpart. > > > > > > Is suspect I need some sort of "goto" if a splice occurs. > > > > > > Mark > > > > > > > Mark, > > > > There are two common options the easiest is probably to put a "next" > > at the end of your if block with the splice. This will more you to > > the next iteration of the while block. last takes you out of the > > block completely--tells while to ignore the rest of > > @fetch=$dbh->fetchrow() and move on to the next $file_item. redo > > tells while to go through the iteration again, with the same value of > > @fetch...which isn't what you want, either. > > > > if ($file ne $database) { > > splice .... ; > > next ; > > } > > > > You could also enclose the rest of the while block in and else clause: > > if ($x) {splice; next } else {do something useful }. That may or may > > not be more difficult to maintain. > > > > Your current code performs the removal, and then continues on with > > whatever code you intend for equal values, probably on the new value > > of $file_items[$count] (which used to be $file_items[$count+1]). > > > > This is why you "skip" two consecutives: When the first one is > > removed, the next one is acted on by the remaining code, and the loop > > continues with the next one after that. > > > > Also, be careful with that auto-increment at the beginning. Your > > current code deletes the item after the one you test. For instance, > > let's assume that that your first description doesn't pass your test: > > > > foreach $file_item ( @file_items ) > > # this takes $file_item[0] and puts it into $file_item > > { > > ($file_item_code,$file_item_description) = split /,/,$file_item ; > > $count ++; > > # $count now == 1 > > > > ...code... > > if ( $file_item_description ne $database_item_description ) > > # test description from $file_items[0] > > { > > splice ( @file_items, $count, 1 ); > > # $file_items[0] ne $database, but removes > > # $file_items[1] because $count++ == 1 > > } > > ...code... > > } > > > > HTH, > > > > --jay > > > > > This is why you "skip" two consecutives: When the first one is > > removed, the next one is acted on by the remaining code, and the loop > > continues with the next one after that. > > make that: the first one is left in place, the second one is removed, > and depending on what the rest of the code looks like, either the > original match ([$count-1], was [$count]) or the new next item > ([$count], was [$count+1]) is processed. > > --j >
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>