Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Sort a matrix by row

by tybalt89 (Monsignor)
on Aug 30, 2022 at 17:28 UTC ( [id://11146503]=note: print w/replies, xml ) Need Help??


in reply to Sort a matrix by row

#!/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"], }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found