ادارہ پاکستان براےء آزاد مصدر

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

Deleting a field

You can also delete fields that you don't want. But you will probably want to check that the field contains what you expect before deleting it. Let's say Enron has gone out of business and the 590 field needs to be deleted:

1 ## Example U3
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','new.dat');
5 open(OUT,'>newer.dat') or die $1;
6
7 while ( my $record = $batch->next() ) {
8
9 ## get the 590 record.
10 my $field = $record->field('590');
11
12 ## if there is a 590 AND it has the word "Enron"...
13 if ($field and $field->as_string() =~ /Enron/i) {
14
15 ## delete it!
16 $record->delete_field($field);
17
18 }
19
20 ## output possibly modified record.
21 print OUT $record->as_usmarc();
22
23 }

The 590 field is retrieved on line 10, but notice how we check that we actually received a valid $field, and that it then contains the word 'Enron' before we delete it. You need to pass delete_field() a MARC::Field object that can be retrieved with the field()method.

Changing existing fields

Perhaps rather than adding or deleting a field, you need to modify an existing field. This is achieved in several steps: first, read in the MARC record you want to update, and then the field you're interested in. From there, call the field's update or replace_with methods to modify its contents, and then resave the record. Below is an example of updating existing 590 field's containing the word 'Enron' to indicate that access is now provided through Arthur Andersen:

1 ## Example U4
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','new.dat');
5 open(OUT,'>newer.dat') or die $1;
6
7 while ( my $record = $batch->next() ) {
8
9 ## look for a 590 containing "Enron"...
10 my $field = $record->field('590');
11 if ($field and $field->as_string =~ /Enron/i) {
12
13 ## create a new 590 field.
14 my $new_field = MARC::Field->new(
15 '590','','', a => 'Access provided by Arthur Andersen.' );
16 $record->delete_field($field);
17 ## replace existing with our new one.
18 $field->replace_with($new_field);
19
20 }
21
22 ## output possibly modified record.
23 print OUT $record->as_usmarc();
24
25 }

In this example, we used MARC::Field's method replace_with() to replace an existing field in the record with a new field that we created. To use replace_with(), you need to retrieve the field you want to replace from a MARC::Record object (line 10), create a new field to replace the existing one with (lines 13-15), and then call the existing field's replace_with() method passing the new field as an argument (lines 18). You must pass replace_with()a valid MARC::Field object.