Hi Leif, It has been almost a year since you posted this piece of marvellous code. I was looking for something like this and had just to copy and paste with some changes and it worked to perfection!
Wanted to thank you for this. Best regards, Saiful Here's my code: ------------------------------- my $batch = new MARC::Batch( 'USMARC', $ARGV[0] ); open (OUTFILE, ">$ARGV[1]"); my @records = (); my @f245 = (); my $idx = 0; while ( my $MARC = $batch->next ) { push(@records, $MARC); push(@f245, [$idx++, $MARC->field("245")->as_string]); } foreach my $rec (sort { $a->[1] cmp $b->[1] } @f245) { print OUTFILE $records[$rec->[0]]->as_usmarc(); } ------------------------------- On 2/1/05, Leif Andersson <[EMAIL PROTECTED]> wrote: > > I am sorry, my earlier posting contained a little error. > The last print statement should read > > print $records[$rec->[0]]->as_usmarc(); > > Jackie also pointed out that his 001 is alphanumeric. > So he changed the comparision operator from "<=>" (numeric) to "cmp" > > The script should have looked like this: > > #!/usr/local/bin/perl -w > use strict; > use MARC::Batch; > > # sort marc records on field 001 > # usage: sort_marc.pl infil.mrc > utfil.mrc > > my $batch = new MARC::Batch( 'USMARC', $ARGV[0] ); > my @records = (); > my @f001 = (); > my $idx = 0; > > while ( my $MARC = $batch->next ) { > push(@records, $MARC); > push(@f001, [$idx++, $MARC->field("001")->as_string]); > } > > foreach my $rec (sort { $a->[1] <=> $b->[1] } @f001) { > # for alphanumeric comparision use: foreach my $rec (sort { $a->[1] cmp > $b->[1] } @f001) { > print $records[$rec->[0]]->as_usmarc(); > } > > __END__ > > Leif -- Saiful Amin Information Specialist Edutech India 8 Khader Nawaz Khan Road Chennai 600006, India Tel: +91 44 2833 0999 GSM: +91 98407 76214 Fax: +91 44 2833 1777 www.edutechindia.com "Enhancing knowledge and skills for success, lifelong."