Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

m// vs grep for counting occurances of a string

by Plankton (Vicar)
on Aug 11, 2007 at 00:59 UTC ( [id://631910]=perlquestion: print w/replies, xml ) Need Help??

Plankton has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: m// vs grep for counting occurances of a string
by wind (Priest) on Aug 11, 2007 at 01:22 UTC

    grep returns the elements of a list for which expression is true. In this case, your list is the array ($line), and your expression is the /$pat/gi. Therefore, @apples will simply be set to ($line) if the pattern /$pat/gi matches, otherwise will be set to nothing.

    Because each of your lines of data has at least one 'apple' in them, the grep count will simply equal the number of lines.

    - Miller

Re: m// vs grep for counting occurances of a string
by GrandFather (Saint) on Aug 11, 2007 at 01:25 UTC

    Read the grep documentation. grep returns a single item ($line) regardless of the number of matches found in the regex so @apples will contain either no elements, or a single element containing $line.

    The regex on the other hand returns an element for each match so you get two elements in @apples for the first line where there are two matches.


    DWIM is Perl's answer to Gödel
      Thank you monks! You just saved me from making a embarrassing bug.
Re: m// vs grep for counting occurances of a string
by dogz007 (Scribe) on Aug 11, 2007 at 01:44 UTC
    If you would like to use grep to find all of the occurences of $pat just like the m//, then split the line before passing to grep, as shown below:

    #!/usr/bin/perl -w use strict; my $count1; my $count2; my $pat = "apple"; my @apples; while (<DATA>) { @apples = grep /$pat/, split ' ', $_; $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

    Prints:

    Count one is 4 Count two is 4
Re: m// vs grep for counting occurances of a string
by mreece (Friar) on Aug 11, 2007 at 05:28 UTC
    incidentally, if you replace grep with map, you get the behavior you were expecting (a count of 4).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://631910]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found