Removing duplicate fields with MARC::record
[email protected] ("Michael Bowden")
| Newsgroups | perl.perl4lib |
|---|---|
| Message-ID | <[email protected]> |
Hi Folks:
Hmmm... I am not sure how my address got in the middle of the code in that last message. Here is a corrected version!
Michael
It is me again. I have another question...
I am helping someone clean up her database. Somehow, the 856 field in her MARC records has duplicated itself several times. She has some records with 15+ duplicate 856 fields. So I am trying, unsuccessfully, to modify one of my scripts to delete the duplicate fields. The problem I am having is that out of the 15+ 856 fields, 3 of the 856 fields are unique to the record and need to stay. Here is my code so far:
## create a MARC::File::USMARC object
use MARC::Batch;
my $infile = shift;
my $otfile = shift;
my $batch = MARC::Batc
## if $file isn't defined we had trouble with the file
## so exit
if (not($infile)) {
print $MARC::File::ERROR,"\n";
exit(0);
}
while (my $record = $batch->next()) {
my @m856 = $record->field('856');
@m856 = sort {$a cmp $b} @m856;
my %seen = ();
my @new856 = ();
foreach ( $record->fields() ) {
if (@m856) {
foreach $f (@m856) {
next if ($seen{ $f }++);
push @new856, $f;
$record->delete_field($f);
}
}
}
$record->insert_fields_ordered( @new856 );
print DBOUT $record->as_usmarc();
}
When I run this script, It put ALL the 856 fields back in the record and they are not sort. What am I doing wrong?
TIA!
Michael
Harrisburg Area Community College