# here is where I messed up for the query. I cannot control this loop. # it finds the things but fails to print them only once. foreach $1 (sort keys %file1){ foreach my $position1 (@{$file1{$1}}){ $found =0; foreach $contig_id(sort keys %hash_1){ foreach my $position (@{$hash_1{$contig_id}}){ $found = 1 if $1 =~ /^$contig_id/ && $contig_id=~ /^$1/ && $position1==$position; print RESULTS "$position1\t$1\n" if $found; print "not matched\n" if !$found; } } } }