I want to sort the second key numerically($a <=> $b) and then store the values(0's and 1's) in an array in the numerical order of the second keys. The goal is to print this array with values tab separated. My output should be like this:$VAR1 = { '315' => { '8' => 0 }, '329' => { '6' => 0 }, '352' => { '5' => 0 }, '390' => { '1' => 0 }, '280' => { '7' => 1 }, '360' => { '9' => 0 }, '349' => { '4' => 0 }, '305' => { '10' => 0 }, '380' => { '3' => 1 }, '251' => { '2' => 0 } };
I already have the header line starting with sample. Just have to place the respective values beneath it. How can I achieve this? This is what works for me but I think there must be an easier way to do it:sample 1 2 3 4 5 6 7 8 9 10 file1 0 0 1 0 0 0 1 0 0 0
my %output=(); foreach my $k1(keys %pos2base2bin){ foreach my $k2 (sort{$a <=> $b} keys %{ $pos2base2bin{$k1} }){ push @{$output{$k2}},$pos2base2bin{$k1}{$k2} } } my @array=(); foreach my $k(sort{$a <=> $b} keys %output){ foreach my $val(@{$output{$k}}){ push @array,$val; } } print join("\t",$f,@array),"\n"; # $f is the filename
In reply to storing values of hash of hashes in an array based on numerical order of second key by v15
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |