in reply to Re^2: Sort a matrix by row
in thread Sort a matrix by row
But then I want to sort alleles according to another array, let's say : my @alleles_origin = (a3,a2,a1);
See for example the replies to the thread How to Order an Array's Elements to Match Another Array's Element Order.
use warnings; use strict; use Data::Dump; my @allel_order = qw/ a3 a2 a1 /; my @matrix = ( [qw/ t1 t1 t2 t2 t1 t2 /], [qw/ a1 a2 a1 a2 a3 a3 /], [qw/ mis mis mis mis del del /], ); my %allel_order = map { $allel_order[$_] => $_ } 0..$#allel_order; my @idx = sort { $matrix[0][$a] cmp $matrix[0][$b] or $allel_order{$matrix[1][$a]} <=> $allel_order{$matrix[1][$b]} } 0..$#{$matrix[0]}; dd @idx; # (4, 1, 0, 5, 3, 2) my @out = map { $matrix[2][$_] } @idx; dd @out; # ("del", "mis", "mis", "del", "mis", "mis")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Sort a matrix by row
by soblanc (Acolyte) on Aug 31, 2022 at 13:45 UTC |