in reply to sort an array according to another array

update: Is there an echo in here? ACK!! i just reread the start of this thread, and realized it was exactly the same thing as the OP is doing in the first place.

Here is my take on your problem. Take the Smaller array and create a hash on what exists in it. Then go through the larger array and grep the elements that exist in the hash created with the smaller array. And in the end it is sorted in the order things appear in the large array and contains only the things in the smaller array, as follows:

use strict; use warnings; my @sorted = ("a" .. "z", "A" .. "Z"); my @needs_sorting = ( qw (P l E a S e s O r T) ); my %S; @S{@needs_sorting} = (); my @is_sorted = grep { exists $S{$_} } @sorted; print join "|",@is_sorted;

Replies are listed 'Best First'.
Re: Re: sort an array ... another array (not reading closely enough)
by wufnik (Friar) on Jun 04, 2003 at 11:12 UTC
    superb. much nicer than my way of getting the subset's sorted order. and still no megahash for the sorted set.

    ...wufnik

    -- in the world of the mules there are no rules --