my ($havar1, $havar2); open(INPUT1, "file1"); open(INPUT2, "file2"); while () { map{push(@{$havar1->{$1}}, $_)} split('_',$2) if ($_ =~ /([^-]+)-(.*)/); } while () { map{push(@{$havar2->{$1}}, $_)} split('_',$2) if ($_ =~ /([^-]+)-(.*)/); } close(INPUT1); close(INPUT2); foreach my $key (keys %{$havar1}) { if (exists $havar2->{$key}) { my $cnt = @{$havar1->{$key}}; my $cnt1 = @{$havar2->{$key}}; for(my $i=0; $i < $cnt; $i=$i+2) { for (my $j=0; $j < $cnt1; $j=$j+2) { if (($havar1->{$key}->[$i] < $havar2->{$key}->[$j]) && ($havar1->{$key}->[$i+1] > $havar2->{$key}->[$j])) { print "$key\t$havar1->{$key}->[$i]\t$havar1->{$key}->[$i+1]\t$key\t$havar2->{$key}->[$j]\t$havar2->{$key}->[$j+1]\n"; } } } } }