for my $found_key ( sort keys %hash1 ) { if ( exists $hash2{ $found_key } ) { ntSpacer( $hash2{ $found_key } ); } } sub ntSpacer { my $seq = shift; ... }