Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

hash of hashes sort second level keys

by aeaton1843 (Acolyte)
on Feb 19, 2010 at 17:08 UTC ( #824207=perlquestion: print w/ replies, xml ) Need Help??
aeaton1843 has asked for the wisdom of the Perl Monks concerning the following question:

I am having problems sorting on keys passed the first level in a hash of hashes. I have tried many things none of which seem to work. Post after post says just sort the keys as I have it below. I have come across a couple of posts, one of which was here suggesting that after the first level the sort sorts the HASH and not the keys of the HASH. Given the following code how do I sort the second level keys.

#!/usr/bin/perl -w use strict; use warnings; my (%sorthash,%test) = (); $sorthash{'10'}{'20'}= '1'; $sorthash{'40'}{'50'}= '4'; $sorthash{'20'}{'30'}= '2'; for my $keys1 (keys %sorthash ) { #print "keys1: $keys1\t"; %test = %{$sorthash{$keys1}}; for my $testkeys (sort keys %test) { print "keys: $keys1\t$testkeys\t"; print "values: $test{$testkeys}\n"; } }
This outputs: keys: 40 50 values: 4 keys: 10 20 values: 1 keys: 20 30 values: 2 instead of: keys: 10 20 values: 1 keys: 20 30 values: 2 keys: 40 50 values: 4

Comment on hash of hashes sort second level keys
Select or Download Code
Re: hash of hashes sort second level keys
by kennethk (Monsignor) on Feb 19, 2010 at 17:32 UTC
    Your issue is that the print order ends up coming from the outer for loop, which returns results based upon the order keys are returned in your outer loop. Do your first level keys always have exactly one second level key? This would make things much easier. Assuming not, you need to collect all second level keys, sort them and then search through all your hashes to find where they occur. Given this is a subroutine, I think the clearest way to do this is to cache the reversed hash results in a local hash and then crawl the results. Something like:

    #!/usr/bin/perl use strict; use warnings; my %sorthash = (); $sorthash{'10'}{'20'}= '1'; $sorthash{'40'}{'50'}= '4'; $sorthash{'20'}{'30'}= '2'; # Invert the hash my %reverse_hash = (); for my $keys1 (keys %sorthash ) { my $child_hashref = $sorthash{$keys1}; for my $keys2 (keys %$child_hashref ) { $reverse_hash{$keys2}{$keys1} = $child_hashref->{$keys2}; } } # Print after sorting keys for my $keys2 (sort {$a <=> $b} keys %reverse_hash) { my $child_hashref = $reverse_hash{$keys2}; for my $keys1 (sort {$a <=> $b} keys %$child_hashref) { print "keys: $keys1\t$keys2\t"; print "values: $child_hashref->{$keys1}\n"; } }

    Some side notes:

    1. Note that the default sort sorts alphabetically not numerically - I noted all your keys were numbers, so I changed the sort.
    2. -w and use warnings; are (nearly) redundant. In general, pick one. * w*, warnings, perllexwarn
    3. Given how you were using %test, it would be reasonable to scope it to the loop rather that at the script level. Scoping can give you good control over bugs and makes variable intent more clear to maintenance folks.
    4. If a hash key contains no spaces, it will be automatically stringified - that means the quotes in your assignments are unnnecessary.

      Simply I tried this and seems to be working:-

      #!/usr/bin/perl use strict; use warnings; my ($sorthash); $sorthash->{'10'}->{'20'}= '1'; $sorthash->{'40'}->{'50'}= '4'; $sorthash->{'20'}->{'30'}= '2'; foreach my $diff (sort {$sorthash->{$b} <=> $sorthash->{$a}} keys %{$s +orthash}) { foreach my $rptNo (keys %{$sorthash->{$diff}}) { print "$diff--- $rptNo\n"; } }

      By: Kuldip Singh Behal

Re: hash of hashes sort second level keys
by rubasov (Friar) on Feb 19, 2010 at 17:49 UTC
    If you flatten your data structure, it would be a lot easier to access the sort key wanted. Of course this is not optimal as you need to duplicate all your data in a temporary array.
    #! /usr/bin/perl use strict; use warnings; my %h; # if your data is really numerical then the apostrophes are superfluou +s $h{10}{20} = 5; $h{10}{25} = 4; $h{05}{50} = 3; $h{20}{30} = 2; my @temp; for my $key1 ( keys %h ) { for my $key2 ( keys %{ $h{$key1} } ) { # create a 3-column table from your hash push @temp, [ $key1, $key2, $h{$key1}{$key2} ]; } } # sort it by the second column numerically for ( sort { $a->[1] <=> $b->[1] } @temp ) { print 'key1=', $_->[0], ', key2=', $_->[1], ', value=', $_->[2], "\n +"; }
    I hope this helps.

    update: a little clarification: By using the solution above you need to store every one of your key1, key2 and value at least once more, but in the terms of memory consumption this can be more than a duplicate.

Re: hash of hashes sort second level keys
by almut (Canon) on Feb 19, 2010 at 18:08 UTC

    You could also use a Schwartzian Transform-like approach, in which you massage your data into a list of first-level,second-level key pairs, which you then sort by index [1] (the second-level key). This also allows to have multiple second-level entries per first-level key.  The first-level key is available in [0], so you can easily reconstruct all data for output:

    #!/usr/bin/perl use strict; use warnings; my %sorthash; $sorthash{10}{20} = 3; $sorthash{10}{40} = 1; $sorthash{40}{50} = 4; $sorthash{20}{30} = 2; print map { "keys: $_->[0]\t$_->[1]\tvalues: $sorthash{$_->[0]}{$_->[1]}\n +" } sort { $a->[1] <=> $b->[1] } map { my $key=$_; map { [$key, $_] } keys %{$sorthash{$key}} } keys %sorthash; __END__ keys: 10 20 values: 3 keys: 20 30 values: 2 keys: 10 40 values: 1 keys: 40 50 values: 4

    Update: note that the approach can in principle be extended to more than two levels. Just create the appropriate [$key1, $key2, $key3, ...] entries (think of spreadsheet-like rows) and sort by whatever column you want.

Re: hash of hashes sort second level keys
by aeaton1843 (Acolyte) on Feb 19, 2010 at 21:33 UTC

    Thanks for all of the replies. I thought the answer may be along the lines of what all of you are saying. My actual problem is quite a bit more complex than this but the answers to this simple question tells me there is no easy way to accomplish what I am trying to do. I actually have a hohref, hr, that is 5 layers deep. I am trying to sort the entire hr on the keys of layer 4 first and layer 3 second. Since the hr is linear, I am stepping through the entire hash recording each set of keys in an array. I then bubble sort the keys keeping track of each relationship and Voila I have the output for which I was looking. Is there a better way to do this?

      While it does increase your complexity, I fail to see why hash flattening or restructuring will not accomplish your goal - the solutions presented here are certainly applicable to your scenario:

      #!/usr/bin/perl use strict; use warnings; my %sorthash = (); $sorthash{'10'}{'20'}{1}{2}{3}= '1'; $sorthash{'40'}{'50'}{1}{4}{3}= '4'; $sorthash{'20'}{'30'}{3}{4}{3}= '2'; # Flatten my @flat_array = (); for my $key1 (keys %sorthash) { for my $key2 (keys %{$sorthash{$key1}} ) { for my $key3 (keys %{$sorthash{$key1}{$key2}} ) { for my $key4 (keys %{$sorthash{$key1}{$key2}{$key3}} ) { for my $key5 (keys %{$sorthash{$key1}{$key2}{$key3}{$k +ey4}} ) { push @flat_array, [$key1, $key2, $key3, $key4, $ke +y5, $sorthash{$key1}{$key2}{$key3}{$key4}{$key5}]; } } } } } # Sort flat array for my $entry (sort { $a->[3] <=> $b->[3] or $a->[2] <=> $b->[2] } @fl +at_array) { print join ", ", @$entry; print "\n"; }

      If that's getting a little deep in terms of nested loops, you can write a recursive crawler to flatten the list:

      #!/usr/bin/perl use strict; use warnings; my %sorthash = (); $sorthash{'10'}{'20'}{1}{2}{3}= '1'; $sorthash{'40'}{'50'}{1}{4}{3}= '4'; $sorthash{'20'}{'30'}{3}{4}{3}= '2'; # Flatten my @flat_array = hash_crawler(\%sorthash); # Sort flat array for my $entry (sort { $a->[3] <=> $b->[3] or $a->[2] <=> $b->[2] } @fl +at_array) { print join ", ", @$entry; print "\n"; } sub hash_crawler { my ($value, @prefix_array) = @_; my @results = (); if (ref $value) { for (keys %$value) { push @results, hash_crawler($value->{$_},@prefix_array,$_) +; } } else { push @results, [@prefix_array, $value]; } return @results; }

      Update: Fixed bug as per rubasov's post below.

        It's not totally clear for me what does it mean "trying to sort the entire hr on the keys of layer 4 first and layer 3 second", but if it means to sort by the 4th column as the key and 3rd column as a subkey, then probably you wanted to write this to sort the flattened array:
        sort { $a->[3] <=> $b->[3] or $a->[2] <=> $b->[2] } @flat_array
Reaped: Re: hash of hashes sort second level keys
by NodeReaper (Curate) on Feb 19, 2010 at 21:38 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://824207]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-27 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls