# Sorting an Array by Another

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

## Re: Sorting an Array by Another

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 = @words;

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

## Re: Sorting an Array by Another

>
> 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 = @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

## Re: Sorting an Array by Another

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 :-)
drleeds@gmail.com

## Re: Sorting an Array by Another

binnyva@hotmail.com (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

--