--- trunk/lib/WebPAC/Normalize.pm 2006/07/23 20:19:56 603 +++ trunk/lib/WebPAC/Normalize.pm 2006/07/30 14:23:23 605 @@ -9,6 +9,7 @@ marc marc_indicators marc_repeatable_subfield marc_compose marc_leader marc_duplicate marc_remove + marc_original_order rec1 rec2 rec regex prefix suffix surround @@ -641,6 +642,71 @@ warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); } +=head2 marc_original_order + +Copy all subfields preserving original order to marc field. + + marc_original_order(210, 260); + +You might want to use this command if you are just renaming subfields or +using pre-processing modify_record in C and don't need any +post-processing or want to preserve order of original subfields. + +=cut + +sub marc_original_order { + + my ($from, $to) = @_; + die "marc_original_order needs from and to fields\n" unless ($from && $to); + + my $r = $rec->{$from} || return; + die "record field $from isn't array\n" unless (ref($r) eq 'ARRAY'); + + my ($i1,$i2) = defined($marc_indicators->{$to}) ? @{ $marc_indicators->{$to} } : (' ',' '); + warn "## marc_original_order($from,$to) source = ", dump( $r ),$/ if ($debug > 1); + + foreach my $d (@$r) { + + if (! defined($d->{subfields}) && ref($d->{subfields}) ne 'ARRAY') { + warn "# marc_original_order($from,$to): field $from doesn't have subfields specification\n"; + next; + } + + my @sfs = @{ $d->{subfields} }; + + die "field $from doesn't have even number of subfields specifications\n" unless($#sfs % 2 == 1); + +warn "#--> d: ",dump($d), "\n#--> sfs: ",dump(@sfs),$/; + + my $m = [ $to, $i1, $i2 ]; + + while (my $sf = shift @sfs) { +warn "#--> sf: ",dump($sf), $/; + my $offset = shift @sfs; + die "corrupted sufields specification for field $from\n" unless defined($offset); + + my $v; + if (ref($d->{$sf}) eq 'ARRAY') { + $v = $d->{$sf}->[$offset] if (defined($d->{$sf}->[$offset])); + } elsif ($offset == 0) { + $v = $d->{$sf}; + } else { + die "field $from subfield '$sf' need occurence $offset which doesn't exist", dump($d->{$sf}); + } + push @$m, ( $sf, $v ) if (defined($v)); + } + + if ($#{$m} > 2) { + push @{ $marc_record->[ $marc_record_offset ] }, $m; + } + } + + warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); + + warn "# marc_original_order is partly implemented"; +} + + =head1 Functions to extract data from input This function should be used inside functions to create C described @@ -924,7 +990,7 @@ my $v = shift @r; warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); - return '' if( ! defined($v) || $v =~ /^\s*$/); + return '' if ( ! defined($v) || $v =~ /^\s*$/); my @s = split( $regex, $v ); warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1);