while (my($name,$n1,$n2)=split(' ',) { # do something... } #### open FILE, "< file2.txt" or die; my @file2= ; # read the whole thing close (FILE); #### foreach my $hit (grep /^$name\w+/, @file2) { my($name_2,$n1_2,$n2_2)=split(' ',$hit); # same as you did before. print "Found $hit\n" if $n1 == $n1_2; }