Re: skip/delete lines with dup keys



Jeff 'japhy' Pinyan wrote:
On Jul 26, Jim said:

Looking for ideas on the best way to skip/delete a line if the LOAN_NO
repeat even if other fields ion in the record are not the same (need to
remove lines with dup key fields).
The code below seems to work, but there must be a better way.


Your code works fine, assuming the duplicates are right after one another.

If not or if you want to avoid sorting then you need to use a hash:

use warnings;
use strict;

# other file opening stuff

my %hash;
while (<F>) {
   chomp;
   my ($id,$score,$bal) = split (/,/, $_);
   next if exists($hash{$id});
   print OUT;
   $hash{$id}++;
}
.