for my $key1 (sort keys %component) { for my $key2 (sort key $component{$key1} ) { #do something with $key1, $key2 and $component{$key1}{$key2} ...