for my $key (sort keys %$data) { # $key should be 1, 2, then 3 here... for my $subject (sort keys {$data->{$key}) { # $subject should be subject1, then subject 2 here if ($subject eq "subject1") { print "$data->{$key}{$subject}\n"; } elsif ($subject eq "subject2" { # subject2 has arrayref of one item, # an href, with two keys: print "$data->{$key}{$subject}[0]{subject3}\n"; print "$data->{$key}{$subject}[0]{subject4}\n"; } } }