: I need to sort all records by: 1) dept, name, record type.
: ...
: 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.

Try a two-pass solution: first create a hash that maps IDs to names,
then use that mapping when you create the sort key:

my (%idToName);
foreach ( @lines ) {
        my ($id,$name) = /^(...)..(.{8})/g;
        next if $name eq "xx      ";
        $idToName{$id} = $name;
}

Also, instead of prepending the sort key to each line, try making it
the value of a hash, which has the line itself as the key.  Something
like this:

my (%sortKeys);
foreach ( @lines ) {
        my $sortKey = ... # do something to create the sort key,
                          # using %idToName to map the ID to the name
        $sortKeys{$_} = $sortKey;
}

Then create a sub to pass to sort:

sub bySortKey {
        $sortKey{$a} cmp $sortKey{$b}
}

--
Tim Kimball · ACDSD / MAST        ¦ 
Space Telescope Science Institute ¦ We are here on Earth to do good to others.
3700 San Martin Drive             ¦ What the others are here for, I don't know.
Baltimore MD 21218 USA            ¦                           -- W.H. Auden

Reply via email to