Sorting an Array by Another

B

Binny V A

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
 
D

drleeds

Binny said:
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;
 
X

xhoster

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
 
A

Anno Siegel

[...]

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
 
D

drleeds

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 :)
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top