469,356 Members | 1,989 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

How to sort lines in an array?

Hi all,

I have another question. I have an array sorted like this:
small_motif_a 1853 1863
small_motif_a 1970 1980
small_motif_a 1971 1981
small_motif_b 789 799
small_motif_b 882 892
small_motif_b 1181 1191
small_motif_b 1193 1203
small_motif_b 1264 1274
and so on

now I want to sort it ascending. I tried it with this code:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Bio::Perl;
  4.  
  5. my @var;
  6. my @lines;
  7. my @sort_col;
  8. @sort_col=();
  9. @lines=();
  10. @var=();
  11. my $column;
  12. my $varfilename;
  13.  
  14. print "Enter the filename of your input file with the variants:= ";        
  15. chomp ($varfilename=<STDIN>);
  16. open (VARINPUT,'<',$varfilename) or die ("$varfilename Can not open file\n");
  17. @var=<VARINPUT>;
  18. #print @var;
  19.  
  20. $column=1; 
  21. while(<VARINPUT>) 
  22.  s/\r?\n//; 
  23.  @var=split /\t/, $_; 
  24.  push @sort_col, $var[$column]; 
  25.  push @lines, "$_\n"; 
  26. @lines=sort {$sort_col[$b] <=> $sort_col[$c] } @lines;
  27. warn "\nSorted $. lines in ascending order, based on numerical values in column $column\n\n"; 
  28. print @lines [sort { $sort_col[$b] <=> $sort_col[$c] } 0..$#sort_col];
  29. print "@lines\n";
  30.  
but it is not working.
Anyone an idea please?
Oct 21 '10 #1
3 1591
toolic
70 Expert
You should use $a and $b (not $b and $c) in your sort block. I'm surprised your code compiles without errors with use strict.
Oct 22 '10 #2
Well, I had it first with $a and $b but I thought it refers to the columns, and I need to sort the 2nd and 3rd. I don't get an output. Only the message that it was sorted
Oct 22 '10 #3
chorny
80 Expert
In first sort you are trying to access array with strings from file as indexes.
Oct 24 '10 #4

Post your reply

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

Similar topics

1 post views Thread by LRW | last post: by
1 post views Thread by Jochen Califice | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
5 posts views Thread by Nel | last post: by
9 posts views Thread by Dylan Parry | last post: by
9 posts views Thread by mamoon | last post: by
pradeepjain
1 post views Thread by pradeepjain | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.