sort function in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort function in perl
# 1  
Old 09-14-2007
Java 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  
Old 09-14-2007
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  
Old 09-14-2007
If in case you want the array sorted numerically, write:
Code:
my @sorted    = sort { $a <=> $b } @array;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inconsistent results using sort function

Could you please advise on the following: I have two space-delimited files with 9 and 10 columns, respectively, with exactly the same values in column 1. However, the order of column 1 differs between the two files, so I want to sort both files by column 1, so that I can align them and... (6 Replies)
Discussion started by: aberg
6 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Sort function UNIX bug ???

Hello there i have a funny behiavor of the sort fonction, i try it out on different Solaris machine and i have the same issue. So i would like to see if there is a rationel explanation here is some data in a file:test.txt ,Test,RSD,RSD_Asset ,Test,RSD,RSD_Credit ,Test,RSD,RSD_Liab... (3 Replies)
Discussion started by: kykyboss
3 Replies

4. Programming

C++ compilation error when I use predicate friend function in the std::sort()

Hi, Can anyone tell me why the following program is giving compiler error when I use a friend function of a class as the comparison predicate for the third parameter of std::sort() algorithm? How to correct it, keep the 'friend' intact? #include <iostream> #include <vector> #include <list>... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

6. Homework & Coursework Questions

Sort function -- My First Unix Homework!

1. The problem statement, all variables and given/known data: To sort a data 2. Relevant commands, code, scripts, algorithms: The data provided is saved in dummy.txt and provided in LIBSVM format, but I think this information is redundant.. +1 1:2 2:4 4:3.2 -1 2:2.1 2:2.1 ... (4 Replies)
Discussion started by: eeweepoh
4 Replies

7. Shell Programming and Scripting

sort - perl

Hi, can I sort the fields on the basis of dates in the following format? from this Dec 17 2007 2:18:18:000PM Dec 17 2007 5:18:18:000AM Jan 19 2009 4:30:02:000AM Mar 21 2010 9:13:55:000AM Dec 16 2007 4:29:21:000PM Dec 24 2009 12:29:23:000PM to Dec 16 2007 4:29:21:000PM... (2 Replies)
Discussion started by: shellwell
2 Replies

8. Shell Programming and Scripting

sort in perl

Hi Is there any way I could reproduce the following code in perl 5.8: sort -u FILE | sort -t: -k1,1 -k2n which sorts by unicity first, then by first key, then by second key in numeric format. What I have now is @sort_array=uniq sort @sort_array; after the contents of my... (5 Replies)
Discussion started by: Indalecio
5 Replies

9. Shell Programming and Scripting

Perl function to sort a file based on key fields

Hi, I am new to PERL.I want to sort all the lines in a file based on 1,2 and 4th filelds. Can U suggest me a command/function in perl for this operation.. (5 Replies)
Discussion started by: karthikd214
5 Replies

10. Programming

sort function

do any one knows where i can find an implementation in c for the sort function (2 Replies)
Discussion started by: dbargo
2 Replies
Login or Register to Ask a Question