in reply to sorting arrays...

When you want to sort a list, other than by a straight lexical or numerical comparision of its elements, the classic approach is the Schawatzian Transform.
# UNTESTED # after opening files @tosort = <TOSORT>; chomp @tosort; @sorted = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ (split('|', $_))[1] , $_ ] } @tosort; for (@sorted) { print SORTED "$_\n" }

The map {} sort {} map {} is read from the bottom up.

The bottom map creates a list of array references, one arrayref per element of @tosort. each arrayref has as it's first element the phone number portion of the element, and as its second element the original element of tosort. The return value of map is a list of these arrayrefs that is fed to the sort.

The sort then sorts these arrayrefs based on comparsion of the first element of each, which is the phone number. The return value of the sort is a list of the now-sorted arrayrefs, which is fed to the top map.

The top map then picks the original values of @tosort out of the arrayrefs. The return value of this map is a list that is put into @sorted.

The Schwarzian transform does the needed sort, with the added advantage that, if the sort criteria is some expensive operation (in this case it was just the split), it is only oden once per element, whereas if it were in the sort, it would be done many more times.

The Schwartzian Transform is named for merlyn, who also goes by the name of Randal Schwartz.

--Bob Niederman, http://bob-n.com