in reply to Re: Complex sort of array of hashes into an array of arrays
in thread Complex sort of array of hashes into an array of arrays

I have the following input data, showing only a few lines:

Pos A C G T 0 4 0 13 0 1 5 0 12 0 2 15 0 2 0 3 0 17 0 0

I stored that data into an array of hashes called @matrix. For instance $matrix[0]{A}=4 and so on.

I want to sort each line from highest frequency to lowest frequency and store it in another array of arrays, but I also need to remember wich letter corresponds to each number

I thought it would be a good idea to store the nucleotide order at the end of the array, here is an example with the first lines that i would like to obtain:

First number is original position. I put it there because then i would like to sort the matrix by the top most score of each row.

[0 13 4 0 0 G A C T] [1 12 5 0 0 G A C T] [2 15 2 0 0 A G C T] [3 17 0 0 0 C A G T]

I hope it's more clear now...

Replies are listed 'Best First'.
Re^3: Complex sort of array of hashes into an array of arrays
by kennethk (Abbot) on Nov 23, 2010 at 16:09 UTC
    Note that your data above is not the same as your original posting - you've added another entry to the start of each row. Do you have code that already reads in your data and generates the actual data structure, or are you just working off input files? Posting code that generates the desired structure makes sure we are working with the same beast you are working with and makes our lives easier. There is no good reason for us to try and guess at what you have and what you need - we are doing you a favor.
Re^3: Complex sort of array of hashes into an array of arrays
by Anonymous Monk on Nov 23, 2010 at 16:09 UTC
    my @sorted = sort { $$a[1] <=> $$b[1]; } map { my $index = $_; my $entry = $data[$index]; my @scores = sort { $$entry{$b} <=> $$entry{$a} keys %$entry; [ $index, @scores, (map { $$entry{$_} } @scores), ] } 0 .. $#data;