An alternative to keeping a separate data structure about your data structure is to create another layer within the existing one. It may make it clearer at a glance which list goes with which list title and make it more maintainable as well.

In this case since we want sorting of the keys of a hash which has values of arrays, I suggest an array of hashes of arrays.

use strict; use warnings; my @array = ( # we need a list { 'Sports' => [ 'Soccer', 'Ultimate Frisbee', 'Basketball' ], }, { 'Books' => [ 'Cannery Row', 'Animal Farm', 'East of Eden' ], }, { 'Places' => [ 'BigSur', 'Zion', 'Crater Lake' ], }, # of name/value pairs # which contain lists as their values ); # you could do this foreach my $hr ( @array ) { my $key = (keys %$hr)[0]; print "$key => " . ( join ', ', @{ $hr->{ $key } } ) . "\n"; } # or maybe this is clearer print "\n"; foreach my $hr ( @array ) { my ( $k, $list ) = each %$hr; print "$k => " . ( join ', ', @$list ) . "\n"; } # or if you really like map print "\n"; print map { (keys $_)[0] . ' => ' . ( join ', ', @{ $_->{ (keys $_)[0] + } } ) . "\n" } @array; # or this print "\n"; print map { my ( $k, $v ) = each %$_; $k . ' => ' . ( join ', ', @$v ) + . "\n" } @array; # or maybe this print "\n"; print map { my $k = (keys %$_)[0]; my $v = join ', ', @{ (values %$_)[ +0] }; "$k => $v\n" } @array; # or maybe even this print "\n"; print map { (keys $_)[0] . ' => ' . ( join ', ', @{ (values %$_)[0] } +) . "\n" } @array;

What I'd really hate to do, though, is have to keep counting items in the hash and items in the separate array to make sure I'm maintaining the right list six months from now. If you're trying to use arrays rather than pull in ordered hash modules, then the proper place for the array is likely in the same data structure as the hash.


In reply to Re^4: How to Sort a Hash According to the Keys in the Hash by mr_mischief
in thread How to Sort a Hash According to the Keys in the Hash by ishootperls

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.