473,700 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1875
toolic
70 Recognized Expert New Member
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 Recognized Expert New Member
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
6410
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() expects parameter 1 to be array, null given in /home/iestud/public_html/gto/search.php on line 43 (line 43 is the sort() function). Here's the page--go to: http://gto.ie-studios.net/item.php?itemid=3
1
1446
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 Polo 1.2" $fahrzeugarray= "57"
7
3261
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)) CType(local4, Short) = CType(src, Short)
5
2099
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 a given area), but once I have the array from mysql I need to do a calculation to find the number of miles in between the store and the user's postcode ($distance). I can do this by looping through the array.
9
2607
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. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
9
6611
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
1861
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 MaxIntegersIndex = 0 For i = 1 To UBound(TheArray) If TheArray(i) > TheArray(MaxIntegersIndex) Then MaxIntegersIndex = i End If
1
1749
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 from 2 different tables.i am getting the output properly but only thing is tht i need to sort the o/p as per name here fid=6 is designation..uid is the id of the user ,fid=3 is Name of the person,fid=11 is for extension ,fid=1 is research...
2
1623
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 will hold the values? i plan on using a interchange sort My second question is that i need to Call a function that will return the most frequently occurring exam (scores from a array) would i use an if statement to do this? there is 25 total...
2
2092
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 array to just the top 50 overall values? If I add, 'LIMIT 50' to this $sql query: $sql = sprintf('SELECT * FROM data WHERE day = CURdate()-INTERVAL 1 day AND statistic_id = %d', $statistic_id);
0
8729
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8648
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9217
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5903
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2031
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.