Velocity Reviews > Perl > Ordering arrays?

# Ordering arrays?

Gary Mayor
Guest
Posts: n/a

 12-17-2003
Hi,
I'm trying to get an array into order i've got this array,
\$number[1] = "5";
\$number[2] = "7";
\$number[3] = "4";
\$number[4] = "8";
\$number[5] = "7";

i'm trying to get it into another array it looks like this,
\$number2[1] = "8";
\$number2[2] = "7";
\$number2[3] = "7";
\$number2[4] = "5";
\$number2[5] = "4";

So far i've got some code like this,
for(\$i=1;\$i<=\$count;\$i++) {
for(\$ii=1;\$ii<=\$count;\$ii++) {
if (\$number[\$ii]>=\$highest[\$i]) {
\$number2 = \$number[\$i];
print "\n\$number2[\$i]";
}
}
}

I've tried the pseudo but could figure out the logic. This is the first
time i've had to do ordering of this sort so i'm a bit stuck at which
way to go next. I've checked out cpan but couldn't find anything that
looked like it would do what i wanted.

Has anyone got any ideas please.

Thanks

Gary

Ryan Shondell
Guest
Posts: n/a

 12-17-2003
Gary Mayor <(E-Mail Removed)> writes:

> Hi,
> I'm trying to get an array into order i've got this array,
> \$number[1] = "5";
> \$number[2] = "7";
> \$number[3] = "4";
> \$number[4] = "8";
> \$number[5] = "7";
>
> i'm trying to get it into another array it looks like this,
> \$number2[1] = "8";
> \$number2[2] = "7";
> \$number2[3] = "7";
> \$number2[4] = "5";
> \$number2[5] = "4";

I think I would do something like this...

@number2 = reverse sort @number1;

Ryan
--
perl -e '\$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
\$;;\$^R.=--\$=*ord for split//,\$~;sub _{for(1..4){\$=
=shift;\$=--if\$=!=4;while(\$=){print chr(ord(\$;[\$%])
+shift);\$%++;\$=--;}print " ";}}_(split//,\$^R);q;;'

Guest
Posts: n/a

 12-17-2003
Gary Mayor wrote:
> I'm trying to get an array into order i've got this array,

That's called sorting, and there is a function to help you do it:

http://www.perldoc.com/perl5.8.0/pod/func/sort.html

This is what I think you are trying to do:

my @number = (5, 7, 4, 8, 7);
my @number2 = sort { \$b <=> \$a } @number;
print "\$_\n" for @number2;

But you obviously need to study some basics about Perl:

http://www.perldoc.com/perl5.8.0/pod/perlintro.html

Good luck!

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gary Mayor
Guest
Posts: n/a

 12-17-2003
Now that is wicked five minutes later i had an answer cheers mate that
works a treat.

Thanks

Gary

Ryan Shondell wrote:
> Gary Mayor <(E-Mail Removed)> writes:
>
>
>>Hi,
>>I'm trying to get an array into order i've got this array,
>>\$number[1] = "5";
>>\$number[2] = "7";
>>\$number[3] = "4";
>>\$number[4] = "8";
>>\$number[5] = "7";
>>
>>i'm trying to get it into another array it looks like this,
>>\$number2[1] = "8";
>>\$number2[2] = "7";
>>\$number2[3] = "7";
>>\$number2[4] = "5";
>>\$number2[5] = "4";

>
>
> I think I would do something like this...
>
> @number2 = reverse sort @number1;
>
> Ryan

Guest
Posts: n/a

 12-17-2003
Ryan Shondell wrote:
> Gary Mayor writes:
>>I'm trying to get an array into order i've got this array,
>>\$number[1] = "5";
>>\$number[2] = "7";
>>\$number[3] = "4";
>>\$number[4] = "8";
>>\$number[5] = "7";
>>
>>i'm trying to get it into another array it looks like this,
>>\$number2[1] = "8";
>>\$number2[2] = "7";
>>\$number2[3] = "7";
>>\$number2[4] = "5";
>>\$number2[5] = "4";

>
> I think I would do something like this...
>
> @number2 = reverse sort @number1;

A lexical sort of numbers? Not a good advice, is it? (It happens to
give the same result as a numerical sort applied on the above data,
since all the values are integers with the same number of digits.)

perldoc -f sort

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Ryan Shondell
Guest
Posts: n/a

 12-17-2003

> A lexical sort of numbers? Not a good advice, is it? (It happens to
> give the same result as a numerical sort applied on the above data,
> since all the values are integers with the same number of digits.)

You're right, of course. I imagine my brain saw the quotes around the
numbers, and went on "lexical autopilot".

@number2 = sort {\$b <=> \$a} @number1;

is a much better solution.

Ryan
--
perl -e '\$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
\$;;\$^R.=--\$=*ord for split//,\$~;sub _{for(1..4){\$=
=shift;\$=--if\$=!=4;while(\$=){print chr(ord(\$;[\$%])
+shift);\$%++;\$=--;}print " ";}}_(split//,\$^R);q;;'

John W. Krahn
Guest
Posts: n/a

 12-18-2003
Gary Mayor wrote:
>
> I'm trying to get an array into order i've got this array,
> \$number[1] = "5";
> \$number[2] = "7";
> \$number[3] = "4";
> \$number[4] = "8";
> \$number[5] = "7";
>
> i'm trying to get it into another array it looks like this,
> \$number2[1] = "8";
> \$number2[2] = "7";
> \$number2[3] = "7";
> \$number2[4] = "5";
> \$number2[5] = "4";

Others have suggested:

@number2 = sort { \$b <=> \$a } @number;

However that populates \$number[0] which I see you don't want. Or did
you think that Perl's arrays started at [1]? Anyway, this will do what
you want:

@number2 = ( undef, sort { \$b <=> \$a } @number );

Or:

@number2[ 1 .. \$#number ] = sort { \$b <=> \$a } @number;

John
--
use Perl;
program
fulfillment

Web Surfer
Guest
Posts: n/a

 12-18-2003
[This followup was posted to comp.lang.perl.misc]

In article <brqhgo\$gc8\$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...
> Hi,
> I'm trying to get an array into order i've got this array,
> \$number[1] = "5";
> \$number[2] = "7";
> \$number[3] = "4";
> \$number[4] = "8";
> \$number[5] = "7";
>
> i'm trying to get it into another array it looks like this,
> \$number2[1] = "8";
> \$number2[2] = "7";
> \$number2[3] = "7";
> \$number2[4] = "5";
> \$number2[5] = "4";
>
> So far i've got some code like this,
> for(\$i=1;\$i<=\$count;\$i++) {
> for(\$ii=1;\$ii<=\$count;\$ii++) {
> if (\$number[\$ii]>=\$highest[\$i]) {
> \$number2 = \$number[\$i];
> print "\n\$number2[\$i]";
> }
> }
> }
>
> I've tried the pseudo but could figure out the logic. This is the first
> time i've had to do ordering of this sort so i'm a bit stuck at which
> way to go next. I've checked out cpan but couldn't find anything that
> looked like it would do what i wanted.
>
> Has anyone got any ideas please.
>
> Thanks
>
> Gary

You should use perl's builtin "sort" function. See "perldoc -f sort"
for further details.