file1: string11 seq33 string123 seq334 string12 seq32 file2: >string11 AGCTAGCTG CAGAGTC >string123 AGCTGAAGA >string12 AGCGATTATCGA AGCATGAAGACC ACAGCATGACTA file3: >seq32 AGAAGCTCCTAGCT >seq334 AGCTGAAGA AAAGCTAGA >seq33 AGGATTCGA AAATATGA my program: open(FILE1,"file1.txt"); open(FILE2,"file2.txt") open(FILE3,"file3.txt") @ray1=;$join1=("",@ray1);@list1=split(">",$join1); @ray2=;$join2=("",@ray1);@list2=split(">",$join2); while($s=){ ($one,$two)=split("\s+",$s); @first=grep($one,@ray1); @second=grep($two,@ray2); # #i should have only 2 sequences here for every loop. }