--- trunk/lib/WebPAC/Normalize.pm 2006/07/02 14:41:01 561 +++ trunk/lib/WebPAC/Normalize.pm 2006/07/02 16:14:41 562 @@ -7,10 +7,13 @@ tag search display marc marc_indicators marc_repeatable_subfield + marc_compose rec1 rec2 rec regex prefix suffix surround first lookup join_with + + split_rec_on /; use warnings; @@ -30,11 +33,11 @@ =head1 VERSION -Version 0.07 +Version 0.08 =cut -our $VERSION = '0.07'; +our $VERSION = '0.08'; =head1 SYNOPSIS @@ -193,6 +196,8 @@ $a->[0] . $a->[3] cmp $b->[0] . $b->[3] } @{ $marc_record }; + @sorted_marc_record = @{ $marc_record }; ### FIXME disable sorting + # output marc fields my @m; @@ -294,6 +299,7 @@ sub _debug { my $l = shift; return $debug unless defined($l); + warn "debug level $l" if ($l > 0); $debug = $l; } @@ -410,6 +416,41 @@ @{ $marc_indicators->{$f} } = ($i1,$i2); } +=head2 marc_compose + +Save values for each MARC subfield explicitly + + marc_compose('900', + 'a', rec('200','a') + 'b', rec('201','a') + 'a', rec('200','b') + 'c', rec('200','c') + ); + +=cut + +sub marc_compose { + my $f = shift or die "marc_compose needs field"; + die "marc_compose field must be numer" unless ($f =~ /^\d+$/); + + my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); + my $m = [ $f, $i1, $i2 ]; + + while (@_) { + my $sf = shift or die "marc_compose $f needs subfield"; + my $v = shift or die "marc_compose $f needs value for subfield $sf"; + + next unless (defined($v) && $v !~ /^\s*$/); + from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); + push @$m, ( $sf, $v ); + warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ) if ($debug > 1); + } + + warn "## marc_compose(d) ", dump( $m ) if ($debug > 1); + + push @{ $marc_record }, $m; +} + =head1 Functions to extract data from input @@ -579,5 +620,39 @@ return join($d, grep { defined($_) && $_ ne '' } @_); } +=head2 split_rec_on + +Split record subfield on some regex and take one of parts out + + $a_before_semi_column = + split_rec_on('200','a', /\s*;\s*/, $part); + +C<$part> is optional number of element. First element is +B<1>, not 0! + +If there is no C<$part> parameter or C<$part> is 0, this function will +return all values produced by splitting. + +=cut + +sub split_rec_on { + die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); + + my ($fld, $sf, $regex, $part) = @_; + warn "### regex ", ref($regex), $regex if ($debug > 2); + + my @r = rec( $fld, $sf ); + my $v = shift @r; + warn "### first rec($fld,$sf) = ",dump($v) if ($debug > 2); + + my @s = split( $regex, $v ); + warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s) if ($debug > 1); + if ($part > 0) { + return $s[ $part - 1 ]; + } else { + return @s; + } +} + # END 1;