--- trunk/lib/WebPAC/Normalize.pm 2006/08/23 14:29:43 616 +++ trunk/lib/WebPAC/Normalize.pm 2006/09/06 21:09:30 642 @@ -4,6 +4,7 @@ _set_rec _set_lookup _get_ds _clean_ds _debug + _pack_subfields_hash tag search display marc marc_indicators marc_repeatable_subfield @@ -36,11 +37,11 @@ =head1 VERSION -Version 0.16 +Version 0.17 =cut -our $VERSION = '0.16'; +our $VERSION = '0.17'; =head1 SYNOPSIS @@ -540,8 +541,12 @@ warn "### marc_compose input subfields = ", dump(@_),$/ if ($debug > 2); + if ($#_ % 2 != 1) { + die "ERROR: marc_compose",dump($f,@_)," not valid (must be even).\nDo you need to add first() or join() around some argument?\n"; + } + while (@_) { - my $sf = shift or die "marc_compose $f needs subfield"; + my $sf = shift; my $v = shift; next unless (defined($v) && $v !~ /^\s*$/); @@ -683,12 +688,13 @@ die "field $from doesn't have even number of subfields specifications\n" unless($#sfs % 2 == 1); -warn "#--> d: ",dump($d), "\n#--> sfs: ",dump(@sfs),$/; + warn "#--> d: ",dump($d), "\n#--> sfs: ",dump(@sfs),$/ if ($debug > 2); my $m = [ $to, $i1, $i2 ]; while (my $sf = shift @sfs) { -warn "#--> sf: ",dump($sf), $/; + + warn "#--> sf: ",dump($sf), $/ if ($debug > 2); my $offset = shift @sfs; die "corrupted sufields specification for field $from\n" unless defined($offset); @@ -709,8 +715,6 @@ } warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); - - warn "# marc_original_order is partly implemented"; } @@ -719,6 +723,41 @@ This function should be used inside functions to create C described above. +=head2 _pack_subfields_hash + + @values = _pack_subfields_hash( $h, $include_subfields ) + +=cut + +sub _pack_subfields_hash { + + warn "## _pack_subfields_hash( ",dump(@_), " )\n" if ($debug > 1); + + my ($h,$include_subfields) = @_; + + + if ( defined($h->{subfields}) ) { + my $sfs = delete $h->{subfields} || die "no subfields?"; + my @out; + while (@$sfs) { + my $sf = shift @$sfs; + push @out, '^' . $sf if ($include_subfields); + my $o = shift @$sfs; + if ($o == 0 && ref( $h->{$sf} ) ne 'ARRAY' ) { + # single element subfields are not arrays + push @out, $h->{$sf}; + } else { +#warn "====> $f $sf $o $#$sfs ", dump( $sfs ), "\n"; + push @out, $h->{$sf}->[$o]; + } + } + return @out; + } else { + # FIXME this should probably be in alphabetical order instead of hash order + values %{$h}; + } +} + =head2 rec1 Return all values in some field @@ -735,13 +774,15 @@ return unless (defined($rec) && defined($rec->{$f})); warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); if (ref($rec->{$f}) eq 'ARRAY') { - return map { - if (ref($_) eq 'HASH') { - values %{$_}; + my @out; + foreach my $h ( @{ $rec->{$f} } ) { + if (ref($h) eq 'HASH') { + push @out, ( _pack_subfields_hash( $h ) ); } else { - $_; + push @out, $h; } - } @{ $rec->{$f} }; + } + return @out; } elsif( defined($rec->{$f}) ) { return $rec->{$f}; }