foreach $file (@one) { my $allfound=0; ... if ($one[2]eq "C4") { $allfound&=1; $xC4=$one[6]; $yC4=$one[7]; $zC4=$one[8]; } if ($one[2]eq "C1") { $allfound&=2; $xC1=$one[6]; $yC1=$one[7]; $zC1=$one[8]; } if ($one[2]eq "C13") { $allfound&=4; $xC13=$one[6]; $yC13=$one[7]; $zC13=$one[8]; } } } close FILETWO; next if ($allfound!=7);