Hi piccard,
I am assuming that you need to sort hashes of AAA, BBB...
The following works if my assumption is correct.
#!/usr/bin/perl
use strict;
use warnings;
my($hash);
$hash->{"AAA"}->{"KEY1"} = "VALUE1";
$hash->{"AAA"}->{"KEY2"} = "VALUE2";
$hash->{"AAA"}->{"KEY3"} = "VALUE3";
$hash->{"BBB"}->{"KEY1"} = "VALUEA";
$hash->{"BBB"}->{"KEY2"} = "VALUEB";
$hash->{"BBB"}->{"KEY3"} = "VALUEC";
for my $set (sort {$hash->{$a}->{"KEY2"} cmp $hash->{$b}->{"KEY2"}} (k
+eys %{$hash})){
print "$set:\n";
for my $key (sort(keys %{$hash->{$set}})){
print "\t$key: $hash->{$set}->{$key}\n";
}
}
The above returns
AAA:
KEY1: VALUE1
KEY2: VALUE2
KEY3: VALUE3
BBB:
KEY1: VALUEA
KEY2: VALUEB
KEY3: VALUEC
sort takes an optional code block which uses the package variables $a and $b for comparison of the members of the list.
Hope that helps
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.