#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11146491;showspoiler=1114 +6492-1 use warnings; use List::AllUtils qw( zip_by sort_by nsort_by ); my @matrix = ( [qw/ t1 t1 t2 t2 t1 t2 /], [qw/ a1 a2 a1 a2 a3 a3 /], [qw/ mis mis mis mis del del /], ); my @alleles_origin = qw( a2 a1 a3 ); my %alleles_sort_order = zip_by { @_ } \@alleles_origin, [ 1 .. @allel +es_origin ]; my @effects = map $_->[2], my @sorted_transpose = sort_by { $_->[0] } # the tN nsort_by { $alleles_sort_order{ $_->[1] } } # the aN zip_by { [ @_ ] } @matrix; use Data::Dump 'dd'; dd { 'sorted_transpose' => \@sorted_transpose, 'wanted effects' => \@effects, 'matrix' => \@matrix };

Outputs:

{ "matrix" => [ ["t1", "t1", "t2", "t2", "t1", "t2"], ["a1", "a2", "a1", "a2", "a3", "a3"], ["mis", "mis", "mis", "mis", "del", "del"], ], "sorted_transpose" => [ ["t1", "a2", "mis"], ["t1", "a1", "mis"], ["t1", "a3", "del"], ["t2", "a2", "mis"], ["t2", "a1", "mis"], ["t2", "a3", "del"], ], "wanted effects" => ["mis", "mis", "del", "mis", "mis", "del"], }

In reply to Re: Sort a matrix by row by tybalt89
in thread Sort a matrix by row by soblanc

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.