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

Trying to get a count of how many times this page has a reg expression match on abcdef. The attempt below keeps giving me a result of 1 hit. Please advise how I can correct this?
use strict; my $url = ''; my $content = get($url); my @files; push(@files,$content); for(@files) { if( $_ =~ /(abcdef)/ ) { print "$1\n"; print (scalar(@files)); } }

Replies are listed 'Best First'.
Re: Hits on a reg expression
by shenme (Priest) on Sep 15, 2003 at 20:29 UTC
    First, are you using LWP::Simple ?   I can't tell from your example.   I'll assume so.   (ah, hope...)

    LWP::Simple::get() will return just one string, containing all the returned lines, into your variable $content so pushing that value onto an array doesn't make sense.

    You may count the instances of one string in another with something like this:

    $n = () = $content =~ m/abcdef/g;
    Reading from right to left, we ask the match operator to match multiple times, we then say we want it to act as though returning a list of all the matched strings, and then we say we _really_ just want the count of matched strings.
Re: Hits on a reg expression
by Mr. Muskrat (Canon) on Sep 15, 2003 at 20:30 UTC

    Replace your for section with this and give it whirl.

    my $total; for(@files) { my $hits = () = $_ =~ /abcdef/g; print $hits,"\n"; $total += $hits; } print "total: $total\n";
      Thanks it works great.
Re: Hits on a reg expression
by Abigail-II (Bishop) on Sep 15, 2003 at 21:22 UTC
    What's this weirdness with @files? You declare the array, add one element to it using push, then loop over it. Why? It's totally unclear to me what your intention is. Do you want to apply the regex to each line of the document you fetched, and count the matches? Do you want to know the number of matches against the entire document? That the above code gives a result of 1 hit is logical - all you do is asking whether abcdef appears in the document. You either get one hit, or no hit at all.


Re: Hits on a reg expression
by Grygonos (Chaplain) on Sep 15, 2003 at 20:13 UTC

    First of all, we need to see the data that should match more than once. For example

    This is my abcdef page. That contains many instances of abcdef.  So far I can count 2 instances of abcdef... oops that makes 3.

    Without knowing what the page looks like and how many hits it should get, it's hard to gauge whether your output of 1 is incorrect. Also, checking the contents of the scalar you are pushing could lead to some interesting discoveries. This may not be your exact code, but if you haven't checked its contents, I would highly reccomend it.

Re: Hits on a reg expression
by asarih (Hermit) on Sep 15, 2003 at 20:31 UTC