473,577 Members | 3,222 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;
  5. my @var;
  6. my @lines;
  7. my @sort_col;
  8. @sort_col=();
  9. @lines=();
  10. @var=();
  11. my $column;
  12. my $varfilename;
  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;
  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";
but it is not working.
Anyone an idea please?
Oct 21 '10 #1
3 1869
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
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

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...
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"
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)
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...
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...
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
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
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...
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...
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 =...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
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...
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...
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...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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...

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.