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
(modified from https://www.perlmonks.org/?node_id=1183136) but they don't work. Thank you very muchmy ( $first_match ) = grep { $line =~ m/\Q$_\E/ } keys %InputData_Empl +oyersHash; print $search and last SEARCH if $first_match ne "";
Here's my working code:
My union.txt file sample: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; } } } } } }
and the collective agreements are just converted text files from PDFs Sample:Sarnia Employees' Bargaining Association Sarnia Municipal Administrative Employees' Assn. Sarnia Police Association Sarnia Professional Fire Fighters'
McMaster University (The "Employer") and Service Employees International Union, Local 2 BGPWU ("SEIU") Representing Hospitality Services Staff ________________________________________ COLLECTIVE AGREEMENT
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |