use feature ':5.10'; open (KEYFILE, "arr2.txt"); @arr2 = ; close(KEYFILE); # Declare and fill up the hash my %elements; foreach (@arr2) { $elements{$_} = 1; }; #I'm repeating this for several quarterly files: for($year=2006; $year<2012; $year=$year+1){ for($i=1; $i<5; $i=$i+1){ # Load each quarterly file $filea = $year . "QTR" . $i . "arr1.txt"; open(MYINFILE, $filea); @arr1 = ; close(MYINFILE); $sizegn = @arr1; # For each entry in the quarterly file for($j=0; $j<$sizegn; $j++){ # Pick only lines that contain the string "txt" if($arr1[$j] =~ m/txt/){ # splits the elements of @arr1, and format the first e +lement into 7-digit number: @arraydata = split(/\|/, $arr1[$j]); my $element = sprintf("%07d", $arraydata[0]); # now check if $element is in hash : if (exists $elements{$element}){ open(MYOUTFILE, ">>Arr3.dat"); print MYOUTFILE $arr1[$j]; close(MYOUTFILE); } print "D'oh - '$element' not found\n" unless (exists $ +elements{$element}); } } } }