foreach my $atom (sort keys %residues{$residue}){ do something with $atom; }