Sorting a hash of hash by second levels keys in Perl -


i have hash follows:

          '4' => {                    '25' => 'x',                    '24' => 'y',                    '23' => 'z'                  },           '3' => {                    '22' => 's',                    '15' => 't'                  },           '2' => {                    '11' => 'a',                    '21' => 'b',                    '9' => 'c',                    '17' => 'd',                    '12' => 'e',                    '20' => 'f',                    '14' => 'g',                    '4' => 'h',                    '18' => 'i',                    '19' => 'j',                    '16' => 'k',                    '10' => 'l',                    '13' => 'm'                  } 

i want sort first on basis of primary key or first key 2,3,4. each of key, 4, want sort secondary key so, expected output be:

          '4' => {                    '23' => 'z',                    '24' => 'y',                    '25' => 'x'                    },           '3' => {                    '15' => 't',                    '22' => 's'                   },           '2' => {                    '4' => 'h',                    '9' => 'c',                    '10' => 'l',                    '11' => 'a',                    '12' => 'e',                    '13' => 'm',                    '14' => 'g',                    '16' => 'k',                    '17' => 'd',                    '18' => 'i',                    '19' => 'j',                    '20' => 'f',                    '21' => 'b'                  } 

i tried code, secondary keys not getting sorted:

foreach $parentkey (sort {$a <=> $b} keys %orderedchoicenamehash) {     $childkey (sort {$orderedchoicenamehash{$a} <=> $orderedchoicenamehash{$b}} keys %{ $orderedchoicenamehash{$parentkey}}) {         print $fh "$parentkey ::::: $childkey ::::: $orderedchoicenamehash{$parentkey}{$childkey}\n";     } } 

foreach $key1 (sort {$b<=>$a} keys %hash) {     foreach $key2 (sort {$a<=>$b} keys %{ $hash{$key1} })     {         print "$key1\t$key2\t$hash{$key1}{$key2}\n";     } } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -