Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2023-02-01 20:02 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (13 votes). Check out past polls.