I have a previous code reading the matrix from a file and storing it in the @matrix array of hashes with the structure I showed in the previous post.

Sorting each row of the original matrix by nucleotide frequency and copying it to another matrix keeping the original position of the row, the nucleotides frequencies and then the nucleotides corresponding to the frequencies in the same order. [OriPos, F1, F2, F3, F4, N1, N2, N3, N4]where F1 is the most frequent nucleotide and N1 is his corresponding letter.

sub sortmatrix(){ for (my $i=0; $i<=$#matrix;$i++){ my @resline = ( $i+1 ); my @keys = (); for my $k ( sort { $matrix[$i]{$b} <=> $matrix[$i]{$a} } keys %{ $ +matrix[$i] } ) { push @keys, $k; push @resline, $matrix[$i]{$k}; } push @resline, @keys; push @sorted, \@resline; # nb. pushing the reference not the array + this time }

Sorting the rows of the matrix by the most frequent nucleotide column, then the 2nd, 3rd, 4th and last by original position

@sorted = sort { $b->[1] <=> $a->[1] || $b->[2] <=> $a->[2] || $b->[3] <=> $a->[3] || $b->[4] <=> $a->[4] || $a->[0] <=> $b->[0]; } @sorted; } use Data::Dumper; print Data::Dumper->Dump([ \@sorted ],[ qw/ *matrix / ]),"\n";

And here i get my matrix completely sorted :)

@sortedmatrix = ( [ 4, 17, 0, 0, 0, 'C', 'A', 'T', 'G' ], [ 5, 17, 0, 0, 0, 'A', 'T', 'C', 'G' ], [ 6, 17, 0, 0, 0, 'T', 'A', 'C', 'G' ], [ 7, 17, 0, 0, 0, 'G', 'A', 'T', 'C' ], [ 9, 17, 0, 0, 0, 'C', 'A', 'T', 'G' ], [ 10, 17, 0, 0, 0, 'C', 'A', 'T', 'G' ], [ 11, 17, 0, 0, 0, 'G', 'A', 'T', 'C' ], [ 12, 17, 0, 0, 0, 'G', 'A', 'T', 'C' ], [ 14, 17, 0, 0, 0, 'C', 'A', 'T', 'G' ], [ 15, 17, 0, 0, 0, 'A', 'T', 'C', 'G' ], [ 16, 17, 0, 0, 0, 'T', 'A', 'C', 'G' ], [ 17, 17, 0, 0, 0, 'G', 'A', 'T', 'C' ], [ 3, 15, 2, 0, 0, 'A', 'G', 'T', 'C' ], [ 13, 15, 2, 0, 0, 'G', 'A', 'T', 'C' ], [ 18, 15, 2, 0, 0, 'T', 'C', 'A', 'G' ], [ 1, 13, 4, 0, 0, 'G', 'A', 'T', 'C' ], [ 8, 13, 4, 0, 0, 'C', 'T', 'A', 'G' ], [ 19, 13, 4, 0, 0, 'C', 'T', 'A', 'G' ], [ 2, 12, 5, 0, 0, 'G', 'A', 'T', 'C' ], [ 20, 7, 7, 2, 0, 'T', 'C', 'G', 'A' ] );

In reply to Re^3: Complex sort of array of hashes into an array of arrays by BioJL
in thread Complex sort of array of hashes into an array of arrays by BioJL

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.