in reply to sorting based on a list

Here's one suggestion using hash slices:

#!/usr/bin/perl -w use strict; my @list = qw(a z b y c x); my @list2 = qw(zulu charlie xray yankee bravo alpha); my %hash; @hash{@list} = @list2; my @sorted = @hash{sort @list}; print "@sorted\n";

Update: OK. Misunderstood the question. In that case it's:

@hash{sort @list} = sort @list2; my @sorted = @hash{@list};

but merlyn's solution is better :(

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: sorting based on a list
by suaveant (Parson) on May 09, 2001 at 18:27 UTC
    This doesn't work... he wants to sort the words in order of their first letters based on the @list array... your thing assigns the characters to the items in list 2, then orders that list and reads them, returning...

    zulu xray bravo alpha yankee charlie
    instead of
    alpha zulu bravo yankee charlie xray
                    - Ant

Re: Re: sorting based on a list
by Chady (Priest) on May 09, 2001 at 18:19 UTC

    what's @hash{@list}?? and wouldn't hasing it up kill duplicates?? wouldn't someone need those duplicates??

    more: I ran this and got: zulu xray bravo alpha yankee charlie is this considered sorted??


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

      @hash{@list} is the list of hash values corresponding to the keys in @list.

      And, yes, it does break if @list contains duplicates.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me