#!/usr/bin/perl -w
BEGIN { $^W = 1 }
use strict;
my %lis;       #age  sex
$lis{cat}{Zoe}=["9","female"];
$lis{dog}{Max}=["8","male"];
$lis{pig}{Joe}=["6","barrow"];
$lis{cat}{Sam}=["1","female"];
$lis{dog}{Bob}=["2","male"];
$lis{cat}{Kat}=["5","female"];
$lis{pig}{Sly}=["7","barrow"];
$lis{cat}{Tom}=["1","male"];
$lis{pig}{Zig}=["3","barrow"];

print "-"x32,"\n";
my %h;
for my $i (keys %lis){
    for my $j (keys %{$lis{$i}}){
        my $s = ", a pet $i named $j; ";
        $h{${$lis{$i}{$j}}[1]}{${$lis{$i}{$j}}[0]}{$s}++;
    }                    #sex                 age
}
for my $h (sort { $a cmp $b } keys %h){
    print "sex $h: ";
    for my $i (sort {$a <=> $b} keys %{$h{$h}}){
        print "age $i";
        for my $s (sort keys %{$h{$h}{$i}}){
            print "$s";
        }
    }
    print "\n";
}
#END

=begin comment text
Subject: [MacPerl] at a loss with hashes
   Date: Mon, 20 Jan 2003 03:01:10 +0100 (MET)
   From: Louis Pouzin <[EMAIL PROTECTED]>
     To: [EMAIL PROTECTED]

Hi, Just trying to understand hash of hash of array.

my %LIS; my $pet='dog';my $x='fido'; my $y=5; my $z='male';
$LIS{$pet}{$x} = [$y,$z];
print "@{$LIS{$pet}{$x}}"; # ok, prints '5 male'

Now, suppose %LIS is populated with lots of pets.
How to get a list of the $z sorted on $y for the dogs ?

my @LIS = sort { ????? }@{$LIS{'dog'}{$x}}; ???

Thanks
=end comment text

Reply via email to