473,411 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1854
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

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

Similar topics

1
by: LRW | last post by:
I have a page that's doing a search of a database, create an array and displays it. And I have it displaying the array just fine, but when I try to sort it, I get the error: Warning: sort()...
1
by: Jochen Califice | last post by:
hello ng! got a problem sorting my multi-dimensional array. I have the following situation of an array: $fahrzeugarray= "23" $fahrzeugarray="Audi TT" $fahrzeugarray= "34" $fahrzeugarray="VW...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
5
by: Nel | last post by:
Hi all, I am using php and mysql to search through a database of stores for the nearest store to a given postcode. I have managed to limit the selection to the nearest stores (roughly within...
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
9
by: mamoon | last post by:
i need a syntax to sort elements of an array to remove duplicacy if any. i tried sort -u to sort a file BUT i need to do this sorting on array. plz help me if it could be. with regard
3
by: viratkothari | last post by:
Public Function MaxValOfIntArray(ByRef TheArray As Variant) As Integer 'This function gives max value of int array without sorting an array Dim i As Integer Dim MaxIntegersIndex As Integer...
1
pradeepjain
by: pradeepjain | last post by:
Hii, i need help in sorting multidimensianal array... i Read the multi sort function cld not use tht so i need help..This is the code. here the problem is tht i need to take out values...
2
by: kaka123 | last post by:
hey im writing a program where i had a question about sorting an array..in my program i have to sort 3 arrays..does this mean i have to do the sort 3 different time and have 3 "dummy" arrays that...
2
by: BaseballGraphs | last post by:
Hello, I am sorting my array so that the values go from high to low. My array contains about 1400 values in it, but I just want to show the top 50 results. How can I go about limiting my sorted...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.