[Bioperl-l] sorting and indexing matricies

Morten Lindow morten at binf.ku.dk
Wed Jun 25 10:31:49 EDT 2003

I think this might help,
(but this _BIO_perl list is probably not the proper place to ask..?)

PD Schloss wrote:

>I have a distance matrix that I am working with and I would like to sort
>it so that the columns and rows containing the smallest distance are in
>the first and second rows and columns.  I understand how to sort a
>simple array, however, I am confused as to how I do it with an array of
>arrays.  I'm a beginner so I realize that constructing a matrix this way
>may not be the most efficient, but at this point I think I would prefer
>to do it this way.  If you have any thoughts on how to do this type of
>sort, I'd appreciate it.
You can use the sort function - there is a good explanation in the 
camel-book, otherwise see this example I use for sorting commaseparated 
text files.


#Read comma separated values from STDIN. Output sorted

use warnings;

my @lines = <STDIN>;
my $header = shift @lines;

my @temp = map { [$_, split /,/] } @lines;

@temp = sort {
        @a_fields = @$a[1..$#$a];
        @b_fields = @$b[1..$#$b];

        $a_fields[1] cmp $b_fields[1]
        $a_fields[3] <=> $b_fields[3]
        $a_fields[0] cmp $b_fields[0]
        } @temp;

my @sorted_lines = map { $_->[0] } @temp;
print STDERR "\nHeader line following:\n$header\n";
print $header;
print @sorted_lines;



More information about the Bioperl-l mailing list