CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sorting

 



sulis
New User

Jan 13, 2005, 5:45 AM

Post #1 of 2 (341 views)
sorting Can't Post

i have a file which looks like this:

01 2 AAAAAA
01 3 AAAAAT
01 4 AAAATT
01 5 AAATTC
01 6 AAAAAA
01 7 AAAATT

i want to lump all the same patterns together. that will look like this:

01 2 AAAAAA
01 6 AAAAAA
01 3 AAAAAT
01 4 AAAATT
01 7 AAAATT
01 5 AAATTC

i did a sort but it din work. anyone can help?


Code
   

foreach $pattern (sort { $a <=> $b } @patterns){

chomp ($pattern);

if ($temp_seq =~ /$pattern/i)
{

if (($temp_pos < $patternLength_decrease) && ($. > 2))
{
print OUTFILE $CHR_NUM,"\t",($exact_Location-

$Length_firstLine-$.+1+1),"\t",$pattern,"\n";
}
else
{
print OUTFILE $CHR_NUM,"\t",($exact_Location-

$Length_firstLine-$.+1),"\t",$pattern,"\n";
}
}
}



davorg
Thaumaturge / Moderator

Jan 13, 2005, 5:54 AM

Post #2 of 2 (340 views)
Re: [sulis] sorting [In reply to] Can't Post

This seems to replicate the results you want, but I'm not sure exactly what your sorting requirements are. Any fixes would need to take place in the "my_sort" subroutine.


Code
#!/usr/bin/perl 

use strict;
use warnings;

foreach (sort my_sort <DATA>) {
print;
}

sub my_sort {
return (split /\s+/, $a)[2] cmp (split /\s+/, $b)[2];
}

__DATA__
01 2 AAAAAA
01 3 AAAAAT
01 4 AAAATT
01 5 AAATTC
01 6 AAAAAA
01 7 AAAATT


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives