CGI/Perl Guide
|
Learning Center
|
Forums
|
Advertise
|
Login
Site Search:
in
Perl Guide
PerlGuru Forums
Learning Ctr
MAIN
INDEX
SEARCH
POSTS
WHO'S
ONLINE
LOG
IN
Home
:
Perl Programming Help
:
Beginner
:
sorting a numerical array
Print Thread
waherne
Novice
Mar 11, 2000, 8:57 AM
Post #1 of 4 (353 views)
sorting a numerical array
Can't Post
Hi,
I have an array e.g. {3,5,6,1,2,4}
and I want to sort it from highest number to lowest to get {6,5,4,3,2,1}.
Any ideas how I do this?
Willie
Cure
User
Mar 11, 2000, 9:06 AM
Post #2 of 4 (353 views)
Re: sorting a numerical array
[
In reply to
]
Can't Post
HI
you can do it this way:
@file = (3,5,6,1,2,4);
@file = @file [sort { $file[$b] <=> $file[$a] } 0 .. $#file ];
print @file;
Cure
japhy
Enthusiast
Mar 11, 2000, 9:24 AM
Post #3 of 4 (353 views)
Re: sorting a numerical array
[
In reply to
]
Can't Post
You don't need to sort via indices there. Just sort via $a and $b:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>
@sorted = sort { $b <=> $a } 1,5,3,7,8,4,2;
</pre><HR></BLOCKQUOTE>
waherne
Novice
Mar 12, 2000, 5:04 AM
Post #4 of 4 (353 views)
Re: sorting a numerical array
[
In reply to
]
Can't Post
Thanks Folks
this worked a treat
Willie
Announcements
PerlGuru Announcements
Perl Programming Help
Frequently Asked Questions
Beginner
Intermediate
Advanced
Regular Expressions
mod_perl
DBI
Win32 Programming Help
Fun With Perl
Perl Quizzes - Learn Perl the Fun Way
Perl Golf
Perl Poetry
Need a Custom or Prewritten Perl Program?
I need a program that...
I Need a Programmer for Freelance Work
Throw Down The Gauntlet
General Discussions
General Questions
Feedback
Tutorial/Article Suggestions for The Learning Cent
Internet Security
Other Programming Languages
Javascript
PHP
Search
this forum
this category
all forums
for
All words
Any words
Whole Phrase
(
options
)
Powered by
Gossamer Forum v.1.2.0
Web Applications
&
Managed Hosting
Powered by Gossamer Threads
Visit our
Mailing List Archives