Skip to content

Instantly share code, notes, and snippets.

@jorol
Created July 31, 2020 13:34
Show Gist options
  • Save jorol/1415fbad2dd344d6b8333bd644f195f9 to your computer and use it in GitHub Desktop.
Save jorol/1415fbad2dd344d6b8333bd644f195f9 to your computer and use it in GitHub Desktop.
Catmandu::Fix::pica_sort.pm
package Catmandu::Fix::pica_sort;
use Catmandu::Sane;
use Moo;
sub fix {
my ( $self, $data ) = @_;
return $self->sort_record($data);
}
sub sort_record {
my ($self, $data) = @_;
if ( defined $data->{record} ) {
my @level_0
= map { $_->{field} }
sort { $a->{tag} cmp $b->{tag} || $a->{occurrence} cmp $b->{occurrence} }
map {
{ tag => $_->[0],
occurrence => defined $_->[1] && length $_->[1] ? $_->[1] : '00',
field => $_
}
} grep { $_->[0] =~ m/^0/ } @{$data->{record}};
$data->{record} = \@level_0;
}
return $data;
}
1;
__END__
=head1 NAME
Catmandu::Fix::pica_sort - sort PICA record level 0 fields by tag and occurence
=head1 SYNOPSIS
# Sort PICA record fields by tag and occurence
pica_sort()
=head1 DESCRIPTION
Sort PICA record fields by tag occurence.
=head1 METHODS
=head2 pica_sort()
If you added new fields to a PICA record with L<Catmandu::Fix::pica_add> use
I<pica_sort> to sort them by tag and occurence.
=head1 SEE ALSO
L<Catmandu::Fix::pica_add>
L<Catmandu::Fix::pica_set>
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment