#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11146491;showspoiler=11146492-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 .. @alleles_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 }; #### { "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"], }