3 |
@EXPORT = qw/ |
@EXPORT = qw/ |
4 |
_set_rec _set_lookup |
_set_rec _set_lookup |
5 |
_get_ds _clean_ds |
_get_ds _clean_ds |
6 |
|
_debug |
7 |
|
|
8 |
tag search display |
tag search display |
9 |
marc21 |
marc marc_indicators marc_repeatable_subfield |
10 |
|
marc_compose marc_leader |
11 |
|
marc_duplicate marc_remove |
12 |
|
|
13 |
rec1 rec2 rec |
rec1 rec2 rec |
14 |
regex prefix suffix surround |
regex prefix suffix surround |
15 |
first lookup join_with |
first lookup join_with |
16 |
|
|
17 |
|
split_rec_on |
18 |
/; |
/; |
19 |
|
|
20 |
use warnings; |
use warnings; |
21 |
use strict; |
use strict; |
22 |
|
|
23 |
#use base qw/WebPAC::Common/; |
#use base qw/WebPAC::Common/; |
24 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
25 |
use Encode qw/from_to/; |
use Encode qw/from_to/; |
26 |
|
use Storable qw/dclone/; |
27 |
|
|
28 |
|
# debugging warn(s) |
29 |
|
my $debug = 0; |
30 |
|
|
31 |
|
|
32 |
=head1 NAME |
=head1 NAME |
33 |
|
|
35 |
|
|
36 |
=head1 VERSION |
=head1 VERSION |
37 |
|
|
38 |
Version 0.06 |
Version 0.10 |
39 |
|
|
40 |
=cut |
=cut |
41 |
|
|
42 |
our $VERSION = '0.06'; |
our $VERSION = '0.10'; |
43 |
|
|
44 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
45 |
|
|
53 |
|
|
54 |
Normalisation can generate multiple output normalized data. For now, supported output |
Normalisation can generate multiple output normalized data. For now, supported output |
55 |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
56 |
C<marc21>. |
C<marc>. |
57 |
|
|
58 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
59 |
|
|
119 |
|
|
120 |
=cut |
=cut |
121 |
|
|
122 |
my $out; |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
123 |
my $marc21; |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
|
my $marc_encoding; |
|
124 |
|
|
125 |
sub _get_ds { |
sub _get_ds { |
126 |
return $out; |
return $out; |
136 |
|
|
137 |
sub _clean_ds { |
sub _clean_ds { |
138 |
my $a = {@_}; |
my $a = {@_}; |
139 |
$out = undef; |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
140 |
$marc21 = undef; |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
141 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
142 |
} |
} |
143 |
|
|
155 |
$lookup = shift; |
$lookup = shift; |
156 |
} |
} |
157 |
|
|
158 |
=head2 _get_marc21_fields |
=head2 _get_marc_fields |
159 |
|
|
160 |
|
Get all fields defined by calls to C<marc> |
161 |
|
|
162 |
|
$marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); |
163 |
|
|
164 |
|
We are using I<magic> which detect repeatable fields only from |
165 |
|
sequence of field/subfield data generated by normalization. |
166 |
|
|
167 |
|
Repeatable field is created when there is second occurence of same subfield or |
168 |
|
if any of indicators are different. |
169 |
|
|
170 |
|
This is sane for most cases. Something like: |
171 |
|
|
172 |
|
900a-1 900b-1 900c-1 |
173 |
|
900a-2 900b-2 |
174 |
|
900a-3 |
175 |
|
|
176 |
|
will be created from any combination of: |
177 |
|
|
178 |
|
900a-1 900a-2 900a-3 900b-1 900b-2 900c-1 |
179 |
|
|
180 |
|
and following rules: |
181 |
|
|
182 |
|
marc('900','a', rec('200','a') ); |
183 |
|
marc('900','b', rec('200','b') ); |
184 |
|
marc('900','c', rec('200','c') ); |
185 |
|
|
186 |
|
which might not be what you have in mind. If you need repeatable subfield, |
187 |
|
define it using C<marc_repeatable_subfield> like this: |
188 |
|
|
189 |
|
marc_repeatable_subfield('900','a'); |
190 |
|
marc('900','a', rec('200','a') ); |
191 |
|
marc('900','b', rec('200','b') ); |
192 |
|
marc('900','c', rec('200','c') ); |
193 |
|
|
194 |
|
will create: |
195 |
|
|
196 |
|
900a-1 900a-2 900a-3 900b-1 900c-1 |
197 |
|
900b-2 |
198 |
|
|
199 |
Get all fields defined by calls to C<marc21> |
There is also support for returning next or specific using: |
200 |
|
|
201 |
$marc->add_fields( WebPAC::Normalize:_get_marc21_fields() ); |
while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) { |
202 |
|
# do something with $mf |
203 |
|
} |
204 |
|
|
205 |
B<TODO>: implement exceptions to magic which unrolls repeated subfields |
will always return fields from next MARC record or |
206 |
as new field with that subfield. |
|
207 |
|
my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 ); |
208 |
|
|
209 |
|
will return 42th copy record (if it exists). |
210 |
|
|
211 |
=cut |
=cut |
212 |
|
|
213 |
sub _get_marc21_fields { |
sub _get_marc_fields { |
214 |
|
|
215 |
|
my $arg = {@_}; |
216 |
|
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
217 |
|
my $offset = $marc_fetch_offset; |
218 |
|
if ($arg->{offset}) { |
219 |
|
$offset = $arg->{offset}; |
220 |
|
} elsif($arg->{fetch_next}) { |
221 |
|
$marc_fetch_offset++; |
222 |
|
} |
223 |
|
|
224 |
|
return if (! $marc_record || ref($marc_record) ne 'ARRAY'); |
225 |
|
|
226 |
|
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
227 |
|
|
228 |
|
my $marc_rec = $marc_record->[ $offset ]; |
229 |
|
|
230 |
|
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
231 |
|
|
232 |
|
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
233 |
|
|
234 |
|
# first, sort all existing fields |
235 |
|
# XXX might not be needed, but modern perl might randomize elements in hash |
236 |
|
my @sorted_marc_record = sort { |
237 |
|
$a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '') |
238 |
|
} @{ $marc_rec }; |
239 |
|
|
240 |
|
@sorted_marc_record = @{ $marc_rec }; ### FIXME disable sorting |
241 |
|
|
242 |
|
# output marc fields |
243 |
my @m; |
my @m; |
244 |
my $last; |
|
245 |
foreach my $row (@{ $marc21 }) { |
# count unique field-subfields (used for offset when walking to next subfield) |
246 |
if ($last && ( |
my $u; |
247 |
$last->[0] eq $row->[0] || # check if field is same |
map { $u->{ $_->[0] . ( $_->[3] || '') }++ } @sorted_marc_record; |
248 |
$last->[1] eq $row->[1] || # check for i1 |
|
249 |
$last->[2] eq $row->[2] # and for i2 |
if ($debug) { |
250 |
) ) { |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
251 |
$last->[3]->{ $row->[3] } = $row->[4]; |
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
252 |
warn "## ++ added $row->[0] ^$row->[3]\n"; |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
253 |
next; |
warn "## subfield count = ", dump( $u ), $/; |
254 |
} elsif ($last) { |
} |
255 |
push @m, $last; |
|
256 |
|
my $len = $#sorted_marc_record; |
257 |
|
my $visited; |
258 |
|
my $i = 0; |
259 |
|
my $field; |
260 |
|
|
261 |
|
foreach ( 0 .. $len ) { |
262 |
|
|
263 |
|
# find next element which isn't visited |
264 |
|
while ($visited->{$i}) { |
265 |
|
$i = ($i + 1) % ($len + 1); |
266 |
|
} |
267 |
|
|
268 |
|
# mark it visited |
269 |
|
$visited->{$i}++; |
270 |
|
|
271 |
|
my $row = dclone( $sorted_marc_record[$i] ); |
272 |
|
|
273 |
|
# field and subfield which is key for |
274 |
|
# marc_repeatable_subfield and u |
275 |
|
my $fsf = $row->[0] . ( $row->[3] || '' ); |
276 |
|
|
277 |
|
if ($debug > 1) { |
278 |
|
|
279 |
|
print "### field so far [", $#$field, "] : ", dump( $field ), " ", $field ? 'T' : 'F', $/; |
280 |
|
print "### this [$i]: ", dump( $row ),$/; |
281 |
|
print "### sf: ", $row->[3], " vs ", $field->[3], |
282 |
|
$marc_repeatable_subfield->{ $row->[0] . $row->[3] } ? ' (repeatable)' : '', $/, |
283 |
|
if ($#$field >= 0); |
284 |
|
|
285 |
} |
} |
286 |
|
|
287 |
push @m, $row; |
# if field exists |
288 |
|
if ( $#$field >= 0 ) { |
289 |
|
if ( |
290 |
|
$row->[0] ne $field->[0] || # field |
291 |
|
$row->[1] ne $field->[1] || # i1 |
292 |
|
$row->[2] ne $field->[2] # i2 |
293 |
|
) { |
294 |
|
push @m, $field; |
295 |
|
warn "## saved/1 ", dump( $field ),$/ if ($debug); |
296 |
|
$field = $row; |
297 |
|
|
298 |
|
} elsif ( |
299 |
|
( $row->[3] lt $field->[-2] ) # subfield which is not next (e.g. a after c) |
300 |
|
|| |
301 |
|
( $row->[3] eq $field->[-2] && # same subfield, but not repeatable |
302 |
|
! $marc_repeatable_subfield->{ $fsf } |
303 |
|
) |
304 |
|
) { |
305 |
|
push @m, $field; |
306 |
|
warn "## saved/2 ", dump( $field ),$/ if ($debug); |
307 |
|
$field = $row; |
308 |
|
|
309 |
|
} else { |
310 |
|
# append new subfields to existing field |
311 |
|
push @$field, ( $row->[3], $row->[4] ); |
312 |
|
} |
313 |
|
} else { |
314 |
|
# insert first field |
315 |
|
$field = $row; |
316 |
|
} |
317 |
|
|
318 |
|
if (! $marc_repeatable_subfield->{ $fsf }) { |
319 |
|
# make step to next subfield |
320 |
|
$i = ($i + $u->{ $fsf } ) % ($len + 1); |
321 |
|
} |
322 |
} |
} |
323 |
|
|
324 |
push @m, $last if ($last); |
if ($#$field >= 0) { |
325 |
|
push @m, $field; |
326 |
|
warn "## saved/3 ", dump( $field ),$/ if ($debug); |
327 |
|
} |
328 |
|
|
329 |
return @m; |
return @m; |
330 |
} |
} |
331 |
|
|
332 |
|
=head2 _debug |
333 |
|
|
334 |
|
Change level of debug warnings |
335 |
|
|
336 |
|
_debug( 2 ); |
337 |
|
|
338 |
|
=cut |
339 |
|
|
340 |
|
sub _debug { |
341 |
|
my $l = shift; |
342 |
|
return $debug unless defined($l); |
343 |
|
warn "debug level $l",$/ if ($l > 0); |
344 |
|
$debug = $l; |
345 |
|
} |
346 |
|
|
347 |
=head1 Functions to create C<data_structure> |
=head1 Functions to create C<data_structure> |
348 |
|
|
349 |
Those functions generally have to first in your normalization file. |
Those functions generally have to first in your normalization file. |
398 |
$out->{$name}->{search} = \@o; |
$out->{$name}->{search} = \@o; |
399 |
} |
} |
400 |
|
|
401 |
=head2 marc21 |
=head2 marc_leader |
402 |
|
|
403 |
|
Setup fields within MARC leader or get leader |
404 |
|
|
405 |
|
marc_leader('05','c'); |
406 |
|
my $leader = marc_leader(); |
407 |
|
|
408 |
|
=cut |
409 |
|
|
410 |
|
sub marc_leader { |
411 |
|
my ($offset,$value) = @_; |
412 |
|
|
413 |
|
if ($offset) { |
414 |
|
$out->{' leader'}->{ $offset } = $value; |
415 |
|
} else { |
416 |
|
return $out->{' leader'}; |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
|
=head2 marc |
421 |
|
|
422 |
Save value for MARC field |
Save value for MARC field |
423 |
|
|
424 |
marc21('900','a', rec('200','a') ); |
marc('900','a', rec('200','a') ); |
425 |
|
marc('001', rec('000') ); |
426 |
|
|
427 |
=cut |
=cut |
428 |
|
|
429 |
sub marc21 { |
sub marc { |
430 |
my $f = shift or die "marc21 needs field"; |
my $f = shift or die "marc needs field"; |
431 |
die "marc21 field must be numer" unless ($f =~ /^\d+$/); |
die "marc field must be numer" unless ($f =~ /^\d+$/); |
432 |
|
|
433 |
my $sf = shift or die "marc21 needs subfield"; |
my $sf; |
434 |
|
if ($f >= 10) { |
435 |
|
$sf = shift or die "marc needs subfield"; |
436 |
|
} |
437 |
|
|
438 |
foreach (@_) { |
foreach (@_) { |
439 |
my $v = $_; # make var read-write for Encode |
my $v = $_; # make var read-write for Encode |
440 |
next unless (defined($v) && $v !~ /^\s+$/); |
next unless (defined($v) && $v !~ /^\s*$/); |
441 |
|
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
442 |
|
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
443 |
|
if (defined $sf) { |
444 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ]; |
445 |
|
} else { |
446 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
447 |
|
} |
448 |
|
} |
449 |
|
} |
450 |
|
|
451 |
|
=head2 marc_repeatable_subfield |
452 |
|
|
453 |
|
Save values for MARC repetable subfield |
454 |
|
|
455 |
|
marc_repeatable_subfield('910', 'z', rec('909') ); |
456 |
|
|
457 |
|
=cut |
458 |
|
|
459 |
|
sub marc_repeatable_subfield { |
460 |
|
my ($f,$sf) = @_; |
461 |
|
die "marc_repeatable_subfield need field and subfield!\n" unless ($f && $sf); |
462 |
|
$marc_repeatable_subfield->{ $f . $sf }++; |
463 |
|
marc(@_); |
464 |
|
} |
465 |
|
|
466 |
|
=head2 marc_indicators |
467 |
|
|
468 |
|
Set both indicators for MARC field |
469 |
|
|
470 |
|
marc_indicators('900', ' ', 1); |
471 |
|
|
472 |
|
Any indicator value other than C<0-9> will be treated as undefined. |
473 |
|
|
474 |
|
=cut |
475 |
|
|
476 |
|
sub marc_indicators { |
477 |
|
my $f = shift || die "marc_indicators need field!\n"; |
478 |
|
my ($i1,$i2) = @_; |
479 |
|
die "marc_indicators($f, ...) need i1!\n" unless(defined($i1)); |
480 |
|
die "marc_indicators($f, $i1, ...) need i2!\n" unless(defined($i2)); |
481 |
|
|
482 |
|
$i1 = ' ' if ($i1 !~ /^\d$/); |
483 |
|
$i2 = ' ' if ($i2 !~ /^\d$/); |
484 |
|
@{ $marc_indicators->{$f} } = ($i1,$i2); |
485 |
|
} |
486 |
|
|
487 |
|
=head2 marc_compose |
488 |
|
|
489 |
|
Save values for each MARC subfield explicitly |
490 |
|
|
491 |
|
marc_compose('900', |
492 |
|
'a', rec('200','a') |
493 |
|
'b', rec('201','a') |
494 |
|
'a', rec('200','b') |
495 |
|
'c', rec('200','c') |
496 |
|
); |
497 |
|
|
498 |
|
=cut |
499 |
|
|
500 |
|
sub marc_compose { |
501 |
|
my $f = shift or die "marc_compose needs field"; |
502 |
|
die "marc_compose field must be numer" unless ($f =~ /^\d+$/); |
503 |
|
|
504 |
|
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
505 |
|
my $m = [ $f, $i1, $i2 ]; |
506 |
|
|
507 |
|
while (@_) { |
508 |
|
my $sf = shift or die "marc_compose $f needs subfield"; |
509 |
|
my $v = shift; |
510 |
|
|
511 |
|
next unless (defined($v) && $v !~ /^\s*$/); |
512 |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
513 |
push @{ $marc21 }, [ $f, ' ', ' ', $sf => $v ]; |
push @$m, ( $sf, $v ); |
514 |
|
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
515 |
|
} |
516 |
|
|
517 |
|
warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); |
518 |
|
|
519 |
|
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
520 |
|
} |
521 |
|
|
522 |
|
=head2 marc_duplicate |
523 |
|
|
524 |
|
Generate copy of current MARC record and continue working on copy |
525 |
|
|
526 |
|
marc_duplicate(); |
527 |
|
|
528 |
|
Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or |
529 |
|
C<< _get_marc_fields( offset => 42 ) >>. |
530 |
|
|
531 |
|
=cut |
532 |
|
|
533 |
|
sub marc_duplicate { |
534 |
|
my $m = $marc_record->[ -1 ]; |
535 |
|
die "can't duplicate record which isn't defined" unless ($m); |
536 |
|
push @{ $marc_record }, dclone( $m ); |
537 |
|
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
538 |
|
$marc_record_offset = $#{ $marc_record }; |
539 |
|
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
540 |
|
} |
541 |
|
|
542 |
|
=head2 marc_remove |
543 |
|
|
544 |
|
Remove some field or subfield from MARC record. |
545 |
|
|
546 |
|
marc_remove('200'); |
547 |
|
marc_remove('200','a'); |
548 |
|
|
549 |
|
This will erase field C<200> or C<200^a> from current MARC record. |
550 |
|
|
551 |
|
This is useful after calling C<marc_duplicate> or on it's own (but, you |
552 |
|
should probably just remove that subfield definition if you are not |
553 |
|
using C<marc_duplicate>). |
554 |
|
|
555 |
|
FIXME: support fields < 10. |
556 |
|
|
557 |
|
=cut |
558 |
|
|
559 |
|
sub marc_remove { |
560 |
|
my ($f, $sf) = @_; |
561 |
|
|
562 |
|
die "marc_remove needs record number" unless defined($f); |
563 |
|
|
564 |
|
my $marc = $marc_record->[ $marc_record_offset ]; |
565 |
|
|
566 |
|
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
567 |
|
|
568 |
|
foreach my $i ( 0 .. $#{ $marc } ) { |
569 |
|
last unless (defined $marc->[$i]); |
570 |
|
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
571 |
|
if ($marc->[$i]->[0] eq $f) { |
572 |
|
if (! defined $sf) { |
573 |
|
# remove whole field |
574 |
|
splice @$marc, $i, 1; |
575 |
|
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
576 |
|
$i--; |
577 |
|
} else { |
578 |
|
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
579 |
|
my $o = ($j * 2) + 3; |
580 |
|
if ($marc->[$i]->[$o] eq $sf) { |
581 |
|
# remove subfield |
582 |
|
splice @{$marc->[$i]}, $o, 2; |
583 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
584 |
|
# is record now empty? |
585 |
|
if ($#{ $marc->[$i] } == 2) { |
586 |
|
splice @$marc, $i, 1; |
587 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
588 |
|
$i--; |
589 |
|
}; |
590 |
|
} |
591 |
|
} |
592 |
|
} |
593 |
|
} |
594 |
} |
} |
595 |
|
|
596 |
|
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
597 |
|
|
598 |
|
$marc_record->[ $marc_record_offset ] = $marc; |
599 |
|
|
600 |
|
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
601 |
} |
} |
602 |
|
|
603 |
=head1 Functions to extract data from input |
=head1 Functions to extract data from input |
617 |
|
|
618 |
sub rec1 { |
sub rec1 { |
619 |
my $f = shift; |
my $f = shift; |
620 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
621 |
return unless (defined($rec) && defined($rec->{$f})); |
return unless (defined($rec) && defined($rec->{$f})); |
622 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
623 |
if (ref($rec->{$f}) eq 'ARRAY') { |
if (ref($rec->{$f}) eq 'ARRAY') { |
624 |
return map { |
return map { |
625 |
if (ref($_) eq 'HASH') { |
if (ref($_) eq 'HASH') { |
676 |
sub regex { |
sub regex { |
677 |
my $r = shift; |
my $r = shift; |
678 |
my @out; |
my @out; |
679 |
#warn "r: $r\n",Dumper(\@_); |
#warn "r: $r\n", dump(\@_); |
680 |
foreach my $t (@_) { |
foreach my $t (@_) { |
681 |
next unless ($t); |
next unless ($t); |
682 |
eval "\$t =~ $r"; |
eval "\$t =~ $r"; |
770 |
return join($d, grep { defined($_) && $_ ne '' } @_); |
return join($d, grep { defined($_) && $_ ne '' } @_); |
771 |
} |
} |
772 |
|
|
773 |
|
=head2 split_rec_on |
774 |
|
|
775 |
|
Split record subfield on some regex and take one of parts out |
776 |
|
|
777 |
|
$a_before_semi_column = |
778 |
|
split_rec_on('200','a', /\s*;\s*/, $part); |
779 |
|
|
780 |
|
C<$part> is optional number of element. First element is |
781 |
|
B<1>, not 0! |
782 |
|
|
783 |
|
If there is no C<$part> parameter or C<$part> is 0, this function will |
784 |
|
return all values produced by splitting. |
785 |
|
|
786 |
|
=cut |
787 |
|
|
788 |
|
sub split_rec_on { |
789 |
|
die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); |
790 |
|
|
791 |
|
my ($fld, $sf, $regex, $part) = @_; |
792 |
|
warn "### regex ", ref($regex), $regex, $/ if ($debug > 2); |
793 |
|
|
794 |
|
my @r = rec( $fld, $sf ); |
795 |
|
my $v = shift @r; |
796 |
|
warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); |
797 |
|
|
798 |
|
return '' if( ! defined($v) || $v =~ /^\s*$/); |
799 |
|
|
800 |
|
my @s = split( $regex, $v ); |
801 |
|
warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1); |
802 |
|
if ($part && $part > 0) { |
803 |
|
return $s[ $part - 1 ]; |
804 |
|
} else { |
805 |
|
return @s; |
806 |
|
} |
807 |
|
} |
808 |
|
|
809 |
# END |
# END |
810 |
1; |
1; |