Using a hash containing arrays, I want to print out a list sorted over all array elements while maintaining the hash entry name.

To illustrate, I borrowed an example from perldoc/perldsc on Hashes of Arrays

In the last part of the example I can print out the list by creating a temporary list of all the names in the arrays, then sort and print it. However, at that point I no longer have the "family name" hash entry association. So, I then re-iterate through the hash and arrays to match the sorted members list with their original hash entries and print from there.

What I want is something like this:

astro:jetsons, barney:flinstones, bart:simpsons, elroy:jetsons, fred:flintstone, ...

BTW: normally sorting by hash key (and then possibly within an array) is what I want, so I "think" I have the correct data structure. However, sometimes I will want to sort over all items in all arrays.

The code below works but I just can't help to think that there's an easier or better way to do it.
Any help finding the better way is appreciated.

Thanks

#!/usr/bin/perl -w use strict; use Data::Dumper; my %HoA = ( simpsons => [ "homer", "marge", "bart" ], flintstones => [ "fred", "barney" ], jetsons => [ "george", "jane", "elroy", "astro" ], ); # # This is the type of sorting normally needed. # print "Sorted by number of members then name\n"; foreach my $family (sort {@{$HoA{$b}} <=> @{$HoA{$a}} || $a cmp $b } k +eys %HoA){ print " $family: \t", join(", ", sort @{ $HoA{$family} }), "\n"; } # # Sometimes this sort will be needed. # my @members; # Dump all names into a single array foreach my $family (keys %HoA ) { push (@members, @{$HoA{$family}} ); } #Sort it and find it's family association for printing foreach my $name (sort @members) { foreach my $family (keys %HoA) { foreach my $test (@{$HoA{$family}}) { print " $name : $family\n" if ($test eq $name); } } }

In reply to Hash of Arrays - Sort over all array entries by knexus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.