v15 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: storing values of hash of hashes in an array based on numerical order of second key
by BillKSmith (Monsignor) on Aug 17, 2020 at 23:04 UTC | |
|
Re: storing values of hash of hashes in an array based on numerical order of second key
by wazat (Monk) on Aug 18, 2020 at 00:04 UTC | |
|
Re: storing values of hash of hashes in an array based on numerical order of second key
by tybalt89 (Monsignor) on Aug 18, 2020 at 03:32 UTC | |
|
Re: storing values of hash of hashes in an array based on numerical order of second key
by johngg (Canon) on Aug 18, 2020 at 14:30 UTC | |
|
Re: storing values of hash of hashes in an array based on numerical order of second key
by LanX (Saint) on Aug 17, 2020 at 21:43 UTC | |
by perlfan (Parson) on Aug 18, 2020 at 04:34 UTC | |
by LanX (Saint) on Aug 18, 2020 at 04:56 UTC |