The scenario: I have a file with ship id numbers. It gets updated several times a day (by database query) and I want to find all the "new_ships" that have been added to it.

sub incremental_update {
    print "Doing incremental update.\n";
    open $FH, '+<', $ships_file or croak "Cannot open $ships_file!\n";

    # Get all the ships in it
    my @ships = do { local $/; <$FH> };

    my %seen = ( );
    my @new_ships = ( );

    foreach my $item (@ships) { $seen{$item} = 1 }

    # Getall the LLY_NUMS from the table
    $sql = qq{ SELECT LLY_NUM FROM AMVER_VESS WHERE ONPLOT_FLAG = 'T' };
    $sth = $dbh->prepare( $sql );
    $sth->execute or die "Can't execute SQL statement: $DBI::errstr\n";

    while ( my @row = $sth->fetchrow->array ) {
        foreach my $lly_num (@row) {
            unless ( $seen{$lly_num} ) {
                # it isn't in %seen, so add it to @new_ships
                push( @new_ships, $lly_num );
            }
        }
        print $FH "@new_ships\n";
    }
    close $FH;
}


I just want to make sure my logic is right on this. If $lly_num isn't in the %seen hash then it gets added to the @new_ships array. Then all the new ships are written to the file.

A secondary question: When opening the file is +< correct if I want to write back to it or should I use +>> ?

Robert

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to