Hi Perl masters, I am trying to lookup the existence of a list of unions in a bunch of collective agreements. I was told my codes aren't efficient because they are going through an array one by one. I try to use these two lines

my ( $first_match ) = grep { $line =~ m/\Q$_\E/ } keys %InputData_Empl +oyersHash; print $search and last SEARCH if $first_match ne "";
(modified from https://www.perlmonks.org/?node_id=1183136) but they don't work. Thank you very much

Here's my working code:

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; } } } } } }
My union.txt file sample:
Sarnia Employees' Bargaining Association Sarnia Municipal Administrative Employees' Assn. Sarnia Police Association Sarnia Professional Fire Fighters'
and the collective agreements are just converted text files from PDFs Sample:
McMaster University (The "Employer") and Service Employees International Union, Local 2 BGPWU ("SEIU") Representing Hospitality Services Staff ________________________________________ COLLECTIVE AGREEMENT


In reply to How to efficiently search for list of strings in multiple files? by stray_tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.