stray_tachyon has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to efficiently search for list of strings in multiple files?
by kcott (Archbishop) on Aug 18, 2018 at 09:00 UTC | |
|
Re: How to efficiently search for list of strings in multiple files?
by Laurent_R (Canon) on Aug 17, 2018 at 21:18 UTC | |
|
Re: How to efficiently search for list of strings in multiple files?
by clueless newbie (Curate) on Aug 17, 2018 at 22:28 UTC | |
by AnomalousMonk (Archbishop) on Aug 18, 2018 at 16:39 UTC | |
|
Re: How to efficiently search for list of strings in multiple files?
by thanos1983 (Parson) on Aug 17, 2018 at 16:57 UTC | |
by stray_tachyon (Initiate) on Aug 17, 2018 at 18:20 UTC | |
by thanos1983 (Parson) on Aug 17, 2018 at 21:49 UTC |