On Thursday, November 6, 2003, at 08:45 PM, Leif Andersson wrote:
Assume we have a record with two 035 fields 035 -- $91234567 035 -- $a(XX)12345678
Now, this code will get the 035 $9 subfield: $subfield = eval { $record->field('035')->subfield('9') }; @subfields = eval { $record->field('035')->subfield('9') };
But this it will fail getting 035 $a $subfield = eval { $record->field('035')->subfield('a') }; @subfields = eval { $record->field('035')->subfield('a') };
I don't see how this can be. Am I missing something?
D'oh! Of course it fails to find the 035 $a subfield -- the call to $record->field('035') in scalar context only returns the *first* 035 field.
My bad.
As for the problem at hand, map and grep are your friends:
my @fields = $record->field('035'); # --> list of all 035 fields my @possible_subfields = map { $_->subfield('a') } @fields; # --> list of results of calling subfield('a') on all 035 fields my @subfields = grep { defined } @possible_subfields; # --> existing 035 $a subfields only
Or, more succinctly:
@subfields = grep { defined } map { $_->subfield('a') } @fields;
Most Perl programmers are familiar with this idiom, so I wouldn't worry about it being hard to understand.
Paul.
-----Ursprungligt meddelande----- Från: Paul Hoffman [mailto:[EMAIL PROTECTED] Skickat: den 6 november 2003 22:30 Till: Leif Andersson Kopia: [EMAIL PROTECTED] Ämne: Re: Return values from MARC::Record
On Thursday, November 6, 2003, at 01:14 PM, Leif Andersson wrote:
With the same BAD record we try $subfield = eval {
$record->field($tag)->subfield($sub) }
This is the only case where we have to put the code in eval.
Should MARC:: take care of the eval for us? I am beginning to think so.
No, it can't. Just add your own error checking, something like this for example:
my $field = $record->field($tag) || die "No tag '$tag' in record"; $subfield = $field->subfield($sub);
MARC::Record::field has no way of knowing that your code will invoke the method 'subfield' on the value it returns. You could also do it like this if you want to keep things concise:
$subfield = ($record->field($tag) || die "No tag '$tag' in record")->subfield($sub);
But that's getting a wee bit obfuscated.
Paul.
-- Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan [EMAIL PROTECTED] :: [EMAIL PROTECTED] :: http://www.nkuitse.com/
-- Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan [EMAIL PROTECTED] :: [EMAIL PROTECTED] :: http://www.nkuitse.com/