You've got a loop within a loop, when you want just one loop going through both files.
Change the line that reads "for my $file2 (<IN2>){" to "$file2 = <IN2>;", and remove the corresponding bracket for the "for" loop, and you should be OK
This kind of has also been discussed extensively only a few days ago in this thread.
<shameless self="ad"> My solution generalized to an arbitrary number of files and lines per file. Since the requirements here are slightly different, a trivial modification is in order.
</shameless>