SG Edwards wrote:
If I want to count the number of times that a match occurs in a line is there a way of doing this, if there is I have not found it!

e.g.
$line="This is a sentence about matching sentences.\n";

$line=~/sentence/ig;

So I will have matched "sentence" twice and I want to record this.

I can do it like this:

        $line=~/sentence/ig;

        # count the number of instances of this in the sentence
        @split=split(/sentence/,$line);
        [EMAIL PROTECTED];
        $length--;


This code works fine, but if I need to keep each matched term (as I have wild-cards to capture spelling variants).

i.e. output would be:
sentence
sentece

Is there a way to automatically capture each individual match (e.g. using a special array character?)

Is this close to what you want?

#!/usr/bin/perl

use strict;
use warnings;

my $line = "This is a sentence about matching sentences.\n";

my $count = 0;
while( $line =~ /(sentence\w*)/ig ){
  my $lexical = $1;
  $count ++;
  print "$count. $lexical\n";
}
print "\ttotal: $count\n";

__END__


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to