Sorting an Array by Another

Discussion in 'Perl Misc' started by Binny V A, Jan 21, 2005.

  1. Binny V A

    Binny V A Guest

    Hello Everyone,

    How do I sort the '@words' array to give it the value,
    "One", "Two", etc. by using the '@numbers' array? I tried
    to do so in the following way - but it won't work.

    my @words = ( "Five","Ten","Seven","Two","Six","Nine","One","Eight","Three","Four"
    );
    my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );

    my $count = -1;
    @words = sort { $count++; $numbers[$count] <=> $numbers[$count+1]; }
    @words;
    @numbers = sort { $a <=> $b } @numbers;

    print "@numbers\n@words";

    How do I do it?

    Thanking You,
    Binny V A
    http://www.geocities.com/binnyva/code
    Binny V A, Jan 21, 2005
    #1
    1. Advertising

  2. Binny V A

    Guest

    Binny V A wrote:
    > Hello Everyone,
    >
    > How do I sort the '@words' array to give it the value,
    > "One", "Two", etc. by using the '@numbers' array? I tried
    > to do so in the following way - but it won't work.
    >
    > my @words = (

    "Five","Ten","Seven","Two","Six","Nine","One","Eight","Three","Four"
    > );
    > my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );
    >
    > my $count = -1;
    > @words = sort { $count++; $numbers[$count] <=> $numbers[$count+1]; }
    > @words;
    > @numbers = sort { $a <=> $b } @numbers;
    >
    > print "@numbers\n@words";
    >
    > How do I do it?
    >
    > Thanking You,
    > Binny V A
    > http://www.geocities.com/binnyva/code



    #!/usr/bin/perl

    use warnings;
    use strict;

    my @words = (
    "Five","Ten","Seven","Two","Six","Nine","One","Eight","Three","Four");
    my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );

    my %hash_slice;
    @hash_slice{@numbers} = @words;

    print "$_ $hash_slice{$_}\n" for sort {int $a <=> int $b} keys
    %hash_slice;
    , Jan 21, 2005
    #2
    1. Advertising

  3. Binny V A

    Guest

    (Binny V A) wrote:
    > Hello Everyone,
    >
    > How do I sort the '@words' array to give it the value,
    > "One", "Two", etc. by using the '@numbers' array? I tried
    > to do so in the following way - but it won't work.
    >
    > my @words = ( "Five","Ten","Seven","Two","Six","Nine","One","Eight",
    > "Three","Four" );
    > my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );



    my @idx = sort {$numbers[$a] <=> $numbers[$b]} 0..$#numbers;
    @words=@words[@idx];


    (You could inline it all and get rid of @idx, if you really wanted
    to objuscate it.)

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jan 21, 2005
    #3
  4. Binny V A

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > Binny V A wrote:
    > > Hello Everyone,
    > >
    > > How do I sort the '@words' array to give it the value,
    > > "One", "Two", etc. by using the '@numbers' array? I tried
    > > to do so in the following way - but it won't work.
    > >
    > > my @words = (

    > "Five","Ten","Seven","Two","Six","Nine","One","Eight","Three","Four"
    > > );
    > > my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );


    [...]

    The code below is basically a sound approach. A few notes:

    > #!/usr/bin/perl
    >
    > use warnings;
    > use strict;
    >
    > my @words = (
    > "Five","Ten","Seven","Two","Six","Nine","One","Eight","Three","Four");
    > my @numbers= ( "5","10","7","2","6","9","1","8","3","4" );
    >
    > my %hash_slice;


    A slight misnomer. %hash_slice is a hash, not a hash slice. You intend
    to use a slice of it, but I don't think that's not a good reason for
    naming it so.

    > @hash_slice{@numbers} = @words;
    >
    > print "$_ $hash_slice{$_}\n" for sort {int $a <=> int $b} keys


    Why the int() in the comparison?. With the given data it's unnecessary,
    and if someone decided to add "1.5" and "One and a Half", it would get
    out of whack.

    Using a hash slice is probably the standard solution for an indirect
    sort like this, but when hash keys are small integers, as is the case
    here, one can always consider using an array instead.

    Anno
    Anno Siegel, Jan 21, 2005
    #4
  5. Binny V A

    Guest

    Thanks for corrections. I forgot that <=> is a numerical comparison
    operator that will put the arguments into a numerical context.

    I was afraid of getting: 1 10 2 3 4 5 6 7 8 9 due to a comparison of
    strings, but in this case, the strings are implicitly cast to numbers
    for the comparison due to the nature of the <=> operator.

    I'm working towards the right to Hubris, but not quite there yet :)
    , Jan 21, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. markspace
    Replies:
    1
    Views:
    385
    markspace
    Jun 25, 2009
  2. Roedy Green
    Replies:
    1
    Views:
    441
    Roedy Green
    Jun 25, 2009
  3. Replies:
    2
    Views:
    1,419
    James Kanze
    Jul 6, 2010
  4. Dominic Son

    Sorting through an array of an array

    Dominic Son, Oct 13, 2006, in forum: Ruby
    Replies:
    5
    Views:
    84
  5. Andrew Poulos

    Sorting array against other array

    Andrew Poulos, Jan 17, 2007, in forum: Javascript
    Replies:
    4
    Views:
    98
    Andrew Poulos
    Jan 23, 2007
Loading...

Share This Page