The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort and Unique in Perl deepakwins Shell Programming and Scripting 20 02-11-2008 09:25 PM
sort and uniq in perl reggiej Shell Programming and Scripting 4 05-18-2006 11:46 PM
sort a file by date using perl esham Shell Programming and Scripting 4 04-14-2005 02:04 PM
sort function dbargo High Level Programming 2 11-21-2002 05:14 PM
Sort file in perl annececile Shell Programming and Scripting 4 06-21-2002 09:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-14-2007
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Post sort function in perl

Hi,

here is my perl script.This script creates an array and is sorting it using the in-built sort function in perl.


Code:
#!/usr/local/bin/perl
    my number=6;
    my @num_arr=(1,2,3,4,5);
    my @array=(23,"$number","Hello",2.345,@num_arr);

    #printing the array
    print "@array\n";

   #sorting the array
   my @sorted    = sort @array;
   print "Sorted : @sorted\n";

The output is an shown below:

23 6 Hello 2.345 1 2 3 4 5
Sorted : 1 2 2.345 23 3 4 5 6 Hello

Can anyone suggest why the sort function act differently?

With Regards
Dileep Pattayath
  #2 (permalink)  
Old 09-14-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
What is "differently"? It appears expected for me.

From the sort documentation:

Quote:
If SUBNAME or BLOCK is omitted, sorts in standard string comparison order.
It treats everything as string and sorts in ASCII order. So, back to the question, what do you expect to see here?
  #3 (permalink)  
Old 09-14-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
If in case you want the array sorted numerically, write:
Code:
my @sorted    = sort { $a <=> $b } @array;

Closed Thread

Bookmarks

Tags
perl sort

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 12:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0