in reply to Complex sort of array of hashes into an array of arrays

use strict; use warnings; use 5.010; my @hrefs = ( {a=>22, g=>7, c=>51, t=>0}, {a=>9, t=>2, c=>30, g=>0}, ); my @final_data; #Extract each hash from the array: for my $hash_ref (@hrefs) { my %hash = %{$hash_ref}; #Convert the hash into an array of key/value pairs: my @pairs = map [$hash{$_}, $_], keys %hash; #Sort the key/value pairs based on the number: my @sorted_pairs = reverse sort {$a->[0] <=> $b->[0]} @pairs; #Add the numbers to one array and the corresponding #letters to another array: my (@numbers, @letters); for my $pair_ref (@sorted_pairs) { push @numbers, $pair_ref->[0]; push @letters, $pair_ref->[1]; } #Add the letters to the end of the numbers array: push @numbers, @letters; say "@numbers"; #Collect the data in an array: push @final_data, \@numbers; } print "\n"; use Data::Dumper; print Dumper(\@final_data); --output:-- 51 22 7 0 c a g t 30 9 2 0 c a t g $VAR1 = [ [ 51, 22, 7, 0, 'c', 'a', 'g', 't' ], [ 30, 9, 2, 0, 'c', 'a', 't', 'g' ] ];

Replies are listed 'Best First'.
Re^2: Complex sort of array of hashes into an array of arrays
by BioJL (Initiate) on Nov 24, 2010 at 09:59 UTC

    Thank you all for are your answers, I think all of them works for me but more important I learned a lot about sorting and data structures.

    I am working now on adapting that code to my (bigger) program.

    I will post that part of the code once done if you think it may be useful for anyone.

      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' ] );