Perl Sort


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Perl Sort
# 1  
Old 04-21-2010
Perl Sort

I am getting the problem to sort these input in perl.

Code:
INPUT
------
Eddie-3
Michael-1
Nica-4
Montoya-6

Then the program should print the output as follows:
Code:
OUTPUT
-------
Michael-1
Eddie-3
Nica-4
Montoya-6

I am only able to sort to the words but I want to sort to numbers..

My scripts gives this output,which is wrong.


Code:
#!/usr/local/bin/perl
$line=<STDIN>;
while ($line ne "") {
chop($line);
push(@array,$line);
$line=<STDIN>;
}
@array2=sort(@array);
foreach $line (@array2){
print "$line\n";
}

Code:
output-> Eddie-3
Michael-1
Montoya-6
Nica-4

but i Want output like .
Code:
Michael-1
Eddie-3
Nica-4
Montoya-6


Last edited by pludi; 04-21-2010 at 02:13 PM.. Reason: code tags, please...
# 2  
Old 04-21-2010
Code:
$ cat input.txt
Eddie-3
Michael-1
Nica-4
Montoya-6
$ perl -We 'print sort { ($l)=($a=~/-(\d+)/); ($r)=($b=~/-(\d+)/); $l <=> $r; } <>' input.txt
Michael-1
Eddie-3
Nica-4
Montoya-6

# 3  
Old 04-21-2010
Could you please explain a little bit about the code you wrote.
I am not able to understand ... Please
# 4  
Old 04-21-2010
For the command line switches, see perldoc perlrun. -W enables warnings (possible typos), -e tells Perl to use the first argument as a stand-alone script ("oneliner").

For the "diamond operator", see perldoc perlop:
Quote:
The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line
When evaluated in a list context, it returns all the lines at once. Those are then fed into sort.

sort can be invoked in 3 forms: without a special argument, it will do regular alphanumerical sorting, depending on your locale. Alternatively, you can specify your own routing as a code block or a sub. Inside this code block, $a and $b refer to the left and right side of the comparison. From each I extract the digit(s) after the dash, and do a numerical comparison using the <=> operator.

Finally, the resulting, sorted, list is printed again.

It could also have been done using the Schwartzian transform, but 2 additional maps would improve nothing, and might even slow it down.
# 5  
Old 04-21-2010
Thanks Pludi for the convincing information.
Could you please also guide the best site or book which contain examples so that I will learn more about perl
# 6  
Old 04-22-2010
Learning Perl, Programming Perl, any information you might find useful from Perl.org.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in sort variable in perl

Hi, I need help in sorting variables in perl i have two variables and if those two variables are equal then its good. I have a problem here example: variable1= number2 number1 variable2=number1 number2 in my case above both the variables are also equal but the condition is not... (5 Replies)
Discussion started by: rocky1954
5 Replies

2. Shell Programming and Scripting

perl hash sort

I have a hash as below 'C1' => { 'x' => 41.9 , 'y' => 5.79999999999995} 'c2 288' => { 'x' => 428.05 , 'y' => 5.79999999999995} 'turn' => { 'x' => 493.25 , 'y' => 209.85} '0001' => { 'x' => 530.1 , 'y' => 195.7} '000001' => { 'x' => 235.25 , 'y' => 728.15} 'XYZ' => { 'x' => 56.65 , 'y' =>... (6 Replies)
Discussion started by: chakrapani
6 Replies

3. 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

4. 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

5. Shell Programming and Scripting

how to do sort in perl?

Hi, Can anyone advise how to correct my script pls? I'm not getting the correct output =( Details below: Logfile ======== Player Num : 14 Player Name : Jacee Thirkins MVP winner : 1998 End of details.... Player Num : 35 Player Name : Lander Diaz MVP winner : 2002... (1 Reply)
Discussion started by: gholdbhurg
1 Replies

6. Shell Programming and Scripting

perl sort

Hi all, Say i have a array @array=("abc,"a",ab"); I would like to sort them according to the length. Any idea? thanks in advanced. i tried below code, but not work. sub sort_fun{ if (length shift(@_) >= length shift(@_) ){ return 1; } else{ return 0; } } (1 Reply)
Discussion started by: summer_cherry
1 Replies

7. Shell Programming and Scripting

Sort and Unique in Perl

Hi, May I know, if a pipe separated File is large, what is the best method to calculate the unique row count of 3rd column and get a list of unique value of the 3rdcolum? Thanks in advance! (20 Replies)
Discussion started by: deepakwins
20 Replies

8. Shell Programming and Scripting

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. #!/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... (2 Replies)
Discussion started by: DILEEP410
2 Replies

9. Shell Programming and Scripting

sort and uniq in perl

Does anyone have a quick and dirty way of performing a sort and uniq in perl? How an array with data like: this is bkupArr BOLADVICE_VN this is bkupArr MLT6800PROD2A this is bkupArr MLT6800PROD2A this is bkupArr BOLADVICE_VN_7YR this is bkupArr MLT6800PROD2A I want to sort it... (4 Replies)
Discussion started by: reggiej
4 Replies

10. Shell Programming and Scripting

Sort file in perl

Hi, I have an entry file for a perl script from which I need to remove duplicate entry. For example: one:two:three one:four:five two:one:three must become : one:two:three two:one:three The duplicate entry is only the first field. I try many options of sort system command but don't... (4 Replies)
Discussion started by: annececile
4 Replies
Login or Register to Ask a Question