an error in perl program

Hi

I am having a file with 243 lines..
The file format s given below

eg
P25787 hsa03050 1 P20618 hsa03050 1
P25786 hsa03050 1 P49721 hsa03050 1
P54132 hsa03440 1 Q13472 hsa03470 1
Q05513 hsa04530 hsa04910 hsa04930 3 Q04759 hsa04530 hsa04910 hsa04930 3
O43463 hsa00310 1 Q92769 hsa04330 hsa04110 hsa05200 hsa05220 hsa05016 5

Here i have to seperate this file in to two.. ie if the 8 letter alphanumerical word s same in both the ids den it should be printed in one file

eg.

P25787 hsa03050 1 P20618 hsa03050 1
P25786 hsa03050 1 P49721 hsa03050 1
Q05513 hsa04530 hsa04910 hsa04930 3 Q04759 hsa04530 hsa04910 hsa04930 3

The highlighted words shud be same

And the other one if the 8 letter alphanumerical word s different den it shud be printed in a file

eg

P54132 hsa03440 1 Q13472 hsa03470 1
O43463 hsa00310 1 Q92769 hsa04330 hsa04110 hsa05200 hsa05220 hsa05016 5

I wrote a perl code for this but its no working properly
the below given s the code

open(f1IN,"infile1.txt"); 
@id = <f1IN>; 
close(f1IN); 
 
open(f2IN,"infile2.txt"); 
@content = <f2IN>; 
close(f2IN); 
 
open(f3OUT,">same.txt"); 
open(f4OUT,">occurence1.txt");
open(f5OUT,">notsame.txt"); 
foreach my $id (@id) 
{ 
    chomp($id); 
    my $count = 0; 
    foreach my $line (@content)  
    { 
        chomp($line); 
        $line =~ s/(\t|\s{2,})/ /g; 
        while ( $line =~ /hsa$id/gi ) 
        { 
            $count ++; 
        } 
        @line = split(/ /,$line); 
        @count = grep /$id/,@line; 
        if ( scalar(@count) == 2 )  
        { 
            print f3OUT "$line\n";
}
        else
            {
                print f5OUT "$line\n";
            } 
         
    }     
    print f4OUT "$id --> $count\n" 
} 
close(f3OUT); 
close(f4OUT);
close(f5OUT); 

The total number of lines in the input file s 243 so once i get two different files and the total of that 2 files shud come 243 but m getting sone 48,000 lines

pls reply me asap