Jeeva Jeeva - 2 months ago 9
Perl Question

PERL MCE::Grep - Why doesn't mce grep pattern work when dynamically passed in a loop

I am using MCE::Grep module to grep the contents of files.

use MCE::Grep;
my $patt="Jack";
my @arr1 = mce_grep_f { /$patt/ } 'logFile';
chomp(@arr1);
for my $line1 (@arr1){
print "$line1\n";
}


It works fine when executed as a single command as above.

my @arr=undef;
for $i (@list) {
print "List : $i\n";
@arr = mce_grep_f { /$i/ } 'logFile';
chomp(@arr);
for my $line (@arr){
print "$line\n";
}
}


But there is an issue while executing the same in a for loop. I guess it might be some syntax issue. Please share suggestions.

Answer
use strict;
use warnings;

use MCE::Grep;

my @list = ('Jack', 'Hobbie');

my $pat = join('|', @list);
my @arr = mce_grep_f { /$pat/ } 'logFile';

chomp(@arr);

for my $line (@arr){
   print "$line\n";
}