On Thu, Dec 11, 2003 at 05:53:27PM +0100, paul POULAIN wrote:
> I've a MARC::Record I want to parse to change something in every 
> subfield value (something like uc() every value). How to do this ? (I 
> don't need to keep the original record).
> what kind of magic foreach ? could do that ?

Well, it turns out this isn't drop dead simple, but it's also not a
common thing to want to do. Without violating our object encapsulation
(which is easy in Perl), here's how I would uppercase all the fields > 010.
Assuming you've got your existing MARC::Record object in $oldRecord.

    ## create an empty record object to populate
    my $newRecord = MARC::Record->new();

    # go through each field in the existing record
    foreach my $oldField ( $oldRecord->fields() ) { 

        # just reproduce tags < 010 in our new record
        if ( $oldField->tag() < 10 ) {
            $newRecord->append_fields( $oldField );
            next();
        }

        # store our new subfield data in this list
        my @newSubfields = ();

        # go through each subfield code/data pair
        foreach my $pair ( $oldField->subfields() ) { 
            # upper case the data portion and store
            push( @newSubfields, $pair->[0], uc($pair->[1]) );
        }

        # add the new field to our new record
        my $newField = MARC::Field->new(
            $oldField->tag(),
            $oldField->indicator(1),
            $oldField->indicator(2),
            @newSubfields
        );

        $newRecord->append_fields( $newField );

    }

Things could be easier if the subfields() just returned a list instead
of a list of array refs.  I'm guessin this was done because if it 
returned a list the temptation to do %subfields = $r->subfields() would
be too great, which would cause problems with repeating subfields.

//Ed

Reply via email to