Current location: Hot Scripts Forums » Programming Languages » Perl » PERL PROBLEM (with listing numbers)


PERL PROBLEM (with listing numbers)

Reply
  #1 (permalink)  
Old 11-15-03, 01:43 PM
web4d web4d is offline
New Member
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
PERL PROBLEM (with listing numbers)

I have the following array of numbers:

@Numbers=(123, 32, 456, 44, 54345, 434534545);

How can I get an array that gives me out these numbers ordered (rising)?!

( ---
@Numbers_ordered=(32, 44, 123, 456, 54345, 434534545);
--)




PLEASE HELP ME!!
Reply With Quote
  #2 (permalink)  
Old 11-17-03, 09:42 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
You need to use sort. The probem is, by default, sort sorts lexically ( aka asciibetically). You have to override the default search to sort numerically like so:

Code:
#!/usr/bin/perl -w
use strict;

my @numbers = (123, 32, 456, 44, 54345, 434534545);
my @sorted = sort { $a <=> $b } @numbers;
# use can also use $b <=> $a to sort in reverse order

print "Content-Type: Text/HTML\n\n";
print '<pre>' . join(' - ', @sorted) . '</pre>';
Check http://www.perldoc.com/ for more info on sort

~Charlie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl newbie has a problem... fowler23 Perl 4 03-24-10 05:14 PM
New to perl, could use some help Arowana Perl 4 10-24-03 10:49 AM
file download problem ukyankee Perl 6 10-04-03 10:39 PM
Flatfile single Forum with Perl Gaz General HotScripts Site Discussion 1 08-07-03 04:37 AM
Updating a listing and getting rated... amailer Hot Scripts Forum Questions, Suggestions and Feedback 2 06-09-03 09:50 PM


All times are GMT -5. The time now is 04:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.