Here is a shot at perl source and others should follow:

Wags ;)
============================================================================
=============

#!perl -ws
=head1
id
---
   dp
   --
     name
     --------
             T
             -
              Unrelated
              -------------
12301spears  Dstuff
12301xx      Rother stuff
12301xx      Emore stuff
34502smith   Dstuff
34502xx      Eother stuff
34502xx      Rmore stuff
56701jones   Dstuff
56701xx      Rother stuff
56701xx      Emore stuff

and the following info:
chars (1-3) ID #
chars (4-5) dept
chars (6-13) name (when record type is D; other data when record type is E
or R)
chars (14) record type
chars (15+) unrelated data

I need to sort all records by: 1) dept, name, record type.
=cut
my @Sorted;
foreach my $data ( sort {$a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] ||
$a->[3] cmp $b->[3]}
                   map{ [$_,/^.{3}(.{2})(.{8})(.)/] } <DATA> ) {
   push(@Sorted,$data->[0]);                   
 }
foreach ( @Sorted ) {
   printf "%-s", $_;
 }                   
__DATA__
12301spears  Dstuff
12301xx      Rother stuff
12301xx      Emore stuff
34502smith   Dstuff
34502xx      Eother stuff
34502xx      Rmore stuff
56701jones   Dstuff
56701xx      Rother stuff
56701xx      Emore stuff
^--------- data ends here

Output:
[D:/CurrWrka/00CommonPerl] aapl149
56701jones   Dstuff
12301spears  Dstuff
56701xx      Emore stuff
12301xx      Emore stuff
12301xx      Rother stuff
56701xx      Rother stuff
34502smith   Dstuff
34502xx      Eother stuff
34502xx      Rmore stuff
-----Original Message-----
From: Steven.Spears@

dana.com [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 25, 2001 11:24
To: [EMAIL PROTECTED]
Subject: tough sort question


Given the following text file:

12301spears  Dstuff
12301xx      Rother stuff
12301xx      Emore stuff
34502smith   Dstuff
34502xx      Eother stuff
34502xx      Rmore stuff
56701jones   Dstuff
56701xx      Rother stuff
56701xx      Emore stuff

and the following info:
chars (1-3) ID #
chars (4-5) dept
chars (6-13) name (when record type is D; other data when record type is E
or R)
chars (14) record type
chars (15+) unrelated data

I need to sort all records by: 1) dept, name, record type.

I'm comfortable with basic sorts, but am unsure how I would go about
'relating' the name with the ID type.
That is, all records beginning with 123 belong to spears, 345 with smith,
etc.,

My first idea is to rebuild the array with a 'sortkey' pre-pended to each
record, and then sorting this temp-array by that key. Then I would remove
the 'sortkey' and have a sorted array.

For example, the temp-array would look like this:

01spears  D12301spears  Dstuff
01spears  E12301xx      Eother stuff
01spears  R12301xx      Rmore stuff
02smith   D34502smith   Dstuff
02smith   E34502xx      Eother stuff
02smith   R34502xx      Rmore stuff
01jones   D56701jones   Dstuff
01jones   E56701xx      Eother stuff
01jones   R56701xx      Rmore stuff

The final output should be:

56701jones   Dstuff
56701xx      Eother stuff
56701xx      Rmore stuff
12301spears  Dstuff
12301xx      Eother stuff
12301xx      Rmore stuff
34502smith   Dstuff
34502xx      Eother stuff
34502xx      Rmore stuff

The real problem I have is with the "name" field being in one record only.
The 'xx' values show that the name cannot be simply copied to the remaining
records. I've looked over Programming Perl and a few FAQ's on sorts, to no
avail.

I've toyed with hashes of arrays, hashes of hashes, and can't work out the
sorting routine.

I'm quite unsure of the 'best' or most 'efficient' way to do this.

I'm looking for any help whatsoever, Thanks!

Steven Spears
(905)-405-0955
[EMAIL PROTECTED]

Reply via email to