Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sort Hash of hashes

by tangent (Parson)
on Mar 22, 2016 at 23:52 UTC ( [id://1158573]=note: print w/replies, xml ) Need Help??


in reply to Sort Hash of hashes

You can use another structure to store the sorted results, in this case a hash of arrays of arrays:
my %sorted; for my $key (keys %bigrams) { my $hash = $bigrams{$key}; for my $secKey (sort { $hash->{$a}<=>$hash->{$b} } keys %$hash) { my $value = $hash->{$secKey} / $freqs{$key}; push( @{ $sorted{$key} }, [$secKey,$value] ); } } while ( my ( $key, $aoa ) = each %sorted ) { print "$key\n"; for my $ary ( @$aoa ) { print "\t$ary->[0] => $ary->[1]\n"; } }
Result:
like VBP => 0.0882352941176471 VB => 0.0882352941176471 IN => 0.823529411764706 kind JJ => 0.2 NN => 0.8 capped VBN => 0.333333333333333 JJ => 0.666666666666667 successful JJ => 1 distribution NN => 1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found