read_in_data_union(); process_files ($FileFolder); sub read_in_data_union { open(my $fh, '<:crlf', $DataFile_Unions) or die "Could not open file '$DataFile_Unions' $!"; chomp(@InputData_Unions = <$fh>); } sub process_files { my $path = shift; opendir (DIR, $path) or die "Unable to open $path: $!"; my @files = grep { /.pdf.txt/ } readdir (DIR); closedir (DIR); @files = map { $path . '/' . $_ } @files; foreach my $file (@files) { open (my $txtfile, $file) or die "error opening $file\n"; print $FileHandle "$file"; LookForUnion: { print $FileHandle "\t"; while (my $line=<$txtfile>) { foreach (@InputData_Unions) { if ($line =~ /\Q$_/i) { print $FileHandle "$_"; last LookForUnion; } } } } } }