in reply to Complex sort of array of hashes into an array of arrays

I have read your post about a dozen times, and I still can't understand what you are trying to do, and if I can't then most other monks here won't either

I think you need to explain a bit further what you are trying to achieve. Can I suggest you post 20 lines or so of raw data, and then the same data sorted by hand in the way you want.

There is almost certainly a simple solution to this using sort, and passing a code block to it.

my @sorted = sort { $a->{$foo}[$bar] <=> $b->{$foo}[$bar] } @raw_data;

Replies are listed 'Best First'.
Re^2: Complex sort of array of hashes into an array of arrays
by BioJL (Initiate) on Nov 23, 2010 at 15:46 UTC

    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...

      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.
      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;