--- trunk/lib/WebPAC/Normalize.pm 2007/11/07 11:54:34 1013 +++ trunk/lib/WebPAC/Normalize.pm 2007/11/10 00:05:36 1019 @@ -16,7 +16,7 @@ marc_template rec1 rec2 rec - frec + frec frec_eq frec_ne regex prefix suffix surround first lookup join_with save_into_lookup @@ -48,7 +48,7 @@ =cut -our $VERSION = '0.33'; +our $VERSION = '0.34'; =head1 SYNOPSIS @@ -815,7 +815,7 @@ return unless defined($rec->{$from}); my $r = $rec->{$from}; - die "record field $from isn't array\n" unless (ref($r) eq 'ARRAY'); + die "record field $from isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY'); my ($i1,$i2) = defined($marc_indicators->{$to}) ? @{ $marc_indicators->{$to} } : (' ',' '); warn "## marc_original_order($to,$from) source = ", dump( $r ),$/ if ($debug > 1); @@ -873,8 +873,8 @@ die "$_ not ARRAY" if ref($args->{$_}) ne 'ARRAY'; } - my $r = $rec->{ $args->{from} }; # || return; - die "record field ", $args->{from}, " isn't array\n" unless (ref($r) eq 'ARRAY'); + my $r = $rec->{ $args->{from} } || return; + die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY'); my @subfields_rename = @{ $args->{subfields_rename} }; # warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename ); @@ -933,9 +933,11 @@ my ( $new_r, $from_count, $to_count ); foreach my $sf ( keys %{$r} ) { + # skip everything which isn't one char subfield (e.g. 'subfields') + next unless $sf =~ m/^\w$/; my $nr = $from_count->{$sf}++; - my $rename_to = $subfields_rename->{ $sf }; # || -# die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename ); + my $rename_to = $subfields_rename->{ $sf } || + die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename ); warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/; my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] }; $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ]; @@ -957,7 +959,10 @@ $fill_in = {}; - foreach my $sf ( split(/\|/, $template ) ) { + my @templates = split(/\|/, $template ); + @templates = ( $template ); + + foreach my $sf ( @templates ) { sub fill_in { my ( $r, $sf, $nr ) = @_; my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] }; @@ -984,7 +989,7 @@ my ( $sf, $nr ) = @$sf; my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; warn "++ $sf/$nr |$v|\n"; - push @$m, [ $sf, $v ]; + push @$m, ( $sf, $v ); } warn "#### >>>> created marc: ", dump( $m ); @@ -993,7 +998,11 @@ } warn "### marc_template produced: ",dump( @marc_out ); - return @marc_out; + + foreach my $marc ( @marc_out ) { + warn "+++ ",dump( $marc ); + push @{ $marc_record->[ $marc_record_offset ] }, $marc; + } } =head2 marc_count @@ -1168,6 +1177,39 @@ return shift @out; } +=head2 frec_eq + +=head2 frec_ne + +Check if first values from two fields are same or different + + if ( frec_eq( 900 => 'a', 910 => 'c' ) ) { + # values are same + } else { + # values are different + } + +Strictly speaking C and C wouldn't be needed if you +could write something like: + + if ( frec( '900','a' ) eq frec( '910','c' ) ) { + # yada tada + } + +but you can't since our parser L will remove all whitespaces +in order to parse text and create invalid function C. + +=cut + +sub frec_eq { + my ( $f1,$sf1, $f2, $sf2 ) = @_; + return (rec( $f1, $sf1 ))[0] eq (rec( $f2, $sf2 ))[0]; +} + +sub frec_ne { + return ! frec_eq( @_ ); +} + =head2 regex Apply regex to some or all values