Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sorting an Array by Another

Reply
Thread Tools

Sorting an Array by Another

 
 
Binny V A
Guest
Posts: n/a
 
      01-21-2005
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","Eig ht","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
 
Reply With Quote
 
 
 
 
drleeds@gmail.com
Guest
Posts: n/a
 
      01-21-2005

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","Eig ht","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","Eig ht","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;

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (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","Eig ht",
> "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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-21-2005
<(E-Mail Removed)> 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","Eig ht","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","Eig ht","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
 
Reply With Quote
 
drleeds@gmail.com
Guest
Posts: n/a
 
      01-21-2005
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 Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
Re: Array objects get changed when sorting the array Roedy Green Java 1 06-25-2009 08:25 PM
Re: Array objects get changed when sorting the array markspace Java 1 06-25-2009 06:22 PM
Sorting array against other array Andrew Poulos Javascript 4 01-23-2007 05:49 AM
Sorting through an array of an array Dominic Son Ruby 5 10-13-2006 11:13 PM



Advertisments