in reply to sorting arrays...

The usual approach to sorting is to read everything that you want to sort (e.g., into an array), sort it, and then save the result. You're reading the unsorted file a line at a time. You can sort that way--by merging each new line into it's correct position in some data structure, but that's more work, and you won't get the benefit of Perl's built-in sort.

Try something like this:

my @unsorted = <TOSORT>; my @sorted = sort byPhone @unsorted;
This lets you isolate details for how records will be compared while sorted into a separate subroutine. (See perldoc -f sort for details). Then, you'll do something like
sub byPhone { return (split(/\|/, $a))[1] cmp (split(/\|/, $b))[1]; }
to extract and compare phone numbers. Exchange $a and $b if you want to reverse the order.

Replies are listed 'Best First'.
Re: Re: sorting arrays...
by iamrobj (Initiate) on Jul 12, 2003 at 22:43 UTC
    Thanks, worked great!:)

    I see that if I change [1] to [0] I can sort by the first item in the array... what is there are 3, 4, or 5 items in the array? Can I keep adding to the byPhone sub? $c, $d etc?

      Can I keep adding to the byPhone sub? $c, $d etc?

      While sorting, $a and $b (and only those two) are special. They've been localized, and hold the two items being compared for sorting purposes. All is explained in perlfunc in the section on sort.

      Since you started with the ActiveState distribution, you'll have all of the core Perl docs available via Start | Applications | ActiveState Perl.