Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ordering arrays?

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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;;'
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
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!

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

 
Reply With Quote
 
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


 
Reply With Quote
 
Gunnar Hjalmarsson
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

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

 
Reply With Quote
 
Ryan Shondell
Guest
Posts: n/a
 
      12-17-2003
Gunnar Hjalmarsson <(E-Mail Removed)> writes:

> 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;;'
 
Reply With Quote
 
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
 
Reply With Quote
 
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.
 
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
Z-Ordering (Morton ordering) question nbigaouette C Programming 2 11-06-2009 05:26 AM
ordering univercd CD Dan Lanciani Cisco 2 06-20-2004 06:58 PM
Access List Entry Ordering mclaughlinj Cisco 1 04-30-2004 04:25 AM
PIX ordering of static statements John Caruso Cisco 9 12-17-2003 06:02 PM
newsgroup ordering Simon Nattrass Firefox 3 12-09-2003 02:41 PM



Advertisments