Hello Monks,
Could someone explain to me what's the difference between using m// vs grep for counting the number of times a pattern appears in text. Here's a same of code to help explain my question:
#!/usr/bin/perl -w
use strict;
my $count1;
my $count2;
my $pat = "apple";
my @apples;
while (<DATA>) {
my $line = $_;
@apples = grep /$pat/gi, $line;
$count1 += @apples;
@apples = m/$pat/gi;
$count2 += @apples;
}
print "Count one is $count1\n";
print "Count two is $count2\n";
__DATA__
awna apple apple glais
16 Uriore Rd apple
Highapplegate corner, VT. 4xyz59
... the output is ...
Count one is 3
Count two is 4
Thanks!