By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

sort and compare

P: 55
Hi All,
I am trying to sort a column based on the numerical value but it seems to me that perl sorts based on first value of the column. For example, some of the data in my file is 23, 5467, 21, 64, 654, 342, 10
When i used the sort function on the array of these numbers, it sorted based on first numerical number and not based on total value. Like it sorted 10, 21, 23,342,5467, 64,654 but i am trying to get based on value like 5467,654,342,64,23,21,10.

Any help will be appreciated.
Thanks
Kumar
Oct 12 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
So, you wanted sorted in two ways, first, but the length of the number, and then a sub-sort to put them in order of each length, largest to smallest (reverse sort).

What code have you tried thus far? You need to show us what you have done before we can help you.

BTW, please put your code in code tags. An example is shown in the "Reply Guidelines" box to the right of the reply window.

Regards,

Jeff
Oct 12 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
to sort numerically you have to use the numeric comparison operator <=>:

Expand|Select|Wrap|Line Numbers
  1. @out = sort {$a <=> $b} @in;
otherwise perl sorts the numbers as ASCII strings, which is the default sort. Its covered in the sort functions documentation.

To reverse the order transpose $a and $b.
Oct 12 '08 #3

P: 55
Thanks for your reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2. open (FH, "cluster4.info");
  3. while(<FH>)
  4. {
  5.     my $line = $_;
  6.     chomp $line;
  7.     if ($line=~/^#/)
  8.         {next;}
  9.     $line=~/.*\s(\d+).*=\s+(\d+)/;
  10.     my $cnum = $1;my $mem = $2;
  11.     $hash{$cnum} = $mem;
  12. }
  13.  
  14.     foreach $value (sort {$hash{$b} cmp $hash{$a} }
  15.            keys %hash)
  16. {
  17.     print "$value $hash{$value}\n";
  18. }
  19.  
Thanks
kumar
Oct 13 '08 #4

P: 55
and here is my sample data
Expand|Select|Wrap|Line Numbers
  1. Cluster 1  Number of members =  23
  2. Cluster 2  Number of members =  285
  3. Cluster 3  Number of members =  4
  4. Cluster 4  Number of members =  28
  5. Cluster 5  Number of members =  1
  6. Cluster 6  Number of members =  24
  7. Cluster 7  Number of members =  54
  8. Cluster 8  Number of members =  246
  9. Cluster 9  Number of members =  1435
  10.  
Thanks
Kumar
Oct 13 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Thanks for your reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2. open (FH, "cluster4.info");
  3. while(<FH>)
  4. {
  5.     my $line = $_;
  6.     chomp $line;
  7.     if ($line=~/^#/)
  8.         {next;}
  9.     $line=~/.*\s(\d+).*=\s+(\d+)/;
  10.     my $cnum = $1;my $mem = $2;
  11.     $hash{$cnum} = $mem;
  12. }
  13.  
  14.     foreach $value (sort {$hash{$b} cmp $hash{$a} }
  15.            keys %hash)
  16. {
  17.     print "$value $hash{$value}\n";
  18. }
  19.  
Thanks
kumar

Is there a question you have about your code?
Oct 13 '08 #6

P: 55
Hi All,
Thanks for your help, I solved the problem of sorting and printing, only a small issue, while printing the data I want to put a new line(only once) when the value is less than 100 but when i am using the if condition its printing in the all the values which i don't want. Any help. I am posting my code, sample data and result output.
Expand|Select|Wrap|Line Numbers
  1. open (FH, "sample4.out") or die "Check the input file";
  2. my $cnt = 0;
  3. while (<FH>)
  4. {
  5.     $n1 = $_;
  6.     if($n1=~ /cluster t.(\d+) has (\d+)/)
  7.     {
  8.         $cid = $1;
  9.         $nc  = $2;
  10.         $hash{$cid} = $nc;
  11.         $cnt++;
  12.     }
  13. }
  14. print "#Total number of cluster = $cnt\n";
  15. close (FH);
  16.  
  17. $count = 1;
  18. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  19. {
  20.     print "$count Cluster $value : Number of members = $hash{$value}\n";if($hash{$value} >= 100){print "\n";}
  21.     $count++;
  22. }
  23.  
Expand|Select|Wrap|Line Numbers
  1. Sample data
  2. #Total number of cluster = 20
  3. Cluster 17  Number of members =  1
  4. Cluster 19  Number of members =  1
  5. Cluster 5  Number of members =  1
  6. Cluster 13  Number of members =  2
  7. Cluster 3  Number of members =  4
  8. Cluster 16  Number of members =  9
  9. Cluster 1  Number of members =  23
  10. Cluster 6  Number of members =  24
  11. Cluster 4  Number of members =  28
  12. Cluster 7  Number of members =  54
  13. Cluster 18  Number of members =  57
  14. Cluster 20  Number of members =  92
  15. Cluster 10  Number of members =  101
  16. Cluster 14  Number of members =  158
  17. Cluster 15  Number of members =  210
  18. Cluster 8  Number of members =  246
  19. Cluster 2  Number of members =  285
  20. Cluster 12  Number of members =  525
  21. Cluster 9  Number of members =  1435
  22. Cluster 11  Number of members =  5744
  23.  
Expand|Select|Wrap|Line Numbers
  1. RESULT
  2. #Total number of cluster = 20
  3. 1 Cluster 11 : Number of members = 5744
  4.  
  5. 2 Cluster 9 : Number of members = 1435
  6.  
  7. 3 Cluster 12 : Number of members = 525
  8.  
  9. 4 Cluster 2 : Number of members = 285
  10.  
  11. 5 Cluster 8 : Number of members = 246
  12.  
  13. 6 Cluster 15 : Number of members = 210
  14.  
  15. 7 Cluster 14 : Number of members = 158
  16.  
  17. 8 Cluster 10 : Number of members = 101
  18.  
  19. 9 Cluster 20 : Number of members = 92
  20. 10 Cluster 18 : Number of members = 57
  21. 11 Cluster 7 : Number of members = 54
  22. 12 Cluster 4 : Number of members = 28
  23. 13 Cluster 6 : Number of members = 24
  24. 14 Cluster 1 : Number of members = 23
  25. 15 Cluster 16 : Number of members = 9
  26. 16 Cluster 3 : Number of members = 4
  27. 17 Cluster 13 : Number of members = 2
  28. 18 Cluster 17 : Number of members = 1
  29. 19 Cluster 19 : Number of members = 1
  30. 20 Cluster 5 : Number of members = 1
  31.  
Thanks
Kumar
Oct 13 '08 #7

Icecrack
Expert 100+
P: 174
Hi All,
Thanks for your help, I solved the problem of sorting and printing, only a small issue, while printing the data I want to put a new line(only once) when the value is less than 100 but when i am using the if condition its printing in the all the values which i don't want. Any help. I am posting my code, sample data and result output.

Thanks
Kumar


can you restate that i don't understand what your getting at.
Oct 13 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
I'm confused. The data you posted does not seem to be the data your script is working with.

Expand|Select|Wrap|Line Numbers
  1. if($n1=~ /cluster t.(\d+) has (\d+)/)
The data is not:

cluster t.nn has nn

where 'nn' is some integer
Oct 13 '08 #9

P: 55
Sorry for the confusion,
actually what I am trying to do is, when the result is printed then, if the number of elements(last column values) are less than 100 then put a new line between the result.
so the result should look something like this:
Expand|Select|Wrap|Line Numbers
  1. #Total number of cluster = 17
  2.  
  3. 1 Cluster 12 : Number of members = 5309
  4. 2 Cluster 8 : Number of members = 1697
  5. 3 Cluster 17 : Number of members = 683
  6. 4 Cluster 1 : Number of members = 400
  7. 5 Cluster 7 : Number of members = 218
  8. 6 Cluster 5 : Number of members = 207
  9. 7 Cluster 16 : Number of members = 173
  10. 8 Cluster 2 : Number of members = 100
  11.  
  12. 9 Cluster 9 : Number of members = 79
  13. 10 Cluster 10 : Number of members = 54
  14. 11 Cluster 3 : Number of members = 53
  15. 12 Cluster 6 : Number of members = 16
  16. 13 Cluster 13 : Number of members = 4
  17. 14 Cluster 14 : Number of members = 3
  18. 15 Cluster 11 : Number of members = 2
  19. 16 Cluster 15 : Number of members = 1
  20. 17 Cluster 4 : Number of members = 1
  21.  
but with my code, its putting new line in every line after the number of elements are less than 100, so my results looks like this
Expand|Select|Wrap|Line Numbers
  1. #Total number of cluster = 17
  2.  
  3. 1 Cluster 12 : Number of members = 5309
  4. 2 Cluster 8 : Number of members = 1697
  5. 3 Cluster 17 : Number of members = 683
  6. 4 Cluster 1 : Number of members = 400
  7. 5 Cluster 7 : Number of members = 218
  8. 6 Cluster 5 : Number of members = 207
  9. 7 Cluster 16 : Number of members = 173
  10. 8 Cluster 2 : Number of members = 100
  11.  
  12. 9 Cluster 9 : Number of members = 79
  13.  
  14. 10 Cluster 10 : Number of members = 54
  15.  
  16. 11 Cluster 3 : Number of members = 53
  17.  
  18. 12 Cluster 6 : Number of members = 16
  19.  
  20. 13 Cluster 13 : Number of members = 4
  21.  
  22. 14 Cluster 14 : Number of members = 3
  23.  
  24. 15 Cluster 11 : Number of members = 2
  25.  
  26. 16 Cluster 15 : Number of members = 1
  27.  
  28. 17 Cluster 4 : Number of members = 1
  29.  
I know that i am printing the new line in the loop but i tried different combinations but neither worked.

Again sorry for the confusion.

Thanks
Kumar
Oct 13 '08 #10

Icecrack
Expert 100+
P: 174
try this if this is what you are asking for?

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  3. {
  4. if ($hash{$value} >= 100){print "\n";}
  5.     print "$count Cluster $value : Number of members = $hash{$value}\n";
  6.     $count++;
  7.  
  8.  
  9. }
Oct 13 '08 #11

nithinpes
Expert 100+
P: 410
If you want to insert an extra newline only once before the values displayed are less than 100, use this approach.
Expand|Select|Wrap|Line Numbers
  1. $count = 1; 
  2. my @high = grep($hash{$_} >= 100, keys %hash);
  3. $hash{$high[$#high]} = $hash{$high[$#high]}. "\n"; #append extra newline
  4.  
  5. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash) 
  6.     print "$count Cluster $value : Number of members = $hash{$value}\n"; 
  7.     $count++; 
  8.  
Oct 13 '08 #12

Icecrack
Expert 100+
P: 174
Ok i think i got what you mean now,

you want the first line that is below or equal to 100 to print a new line before it and thats it.

try:
Expand|Select|Wrap|Line Numbers
  1. foreach $value (sort {$hash{$b} <=> $hash{$a} } keys %hash)
  2. {
  3. $flag=0;
  4. if (($hash{$value} >= 100) && ($flag == 0)
  5. {
  6. print "\n"; $flag=1;
  7. }
  8.     print "$count Cluster $value : Number of members = $hash{$value}\n";
  9.     $count++;
  10. }
Oct 13 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.