Re: Count number of times matched

2005-12-01 Thread Dr.Ruud
SG Edwards:

> I want to count the number of times that a match occurs

  perldoc -q count

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Count number of times matched

2005-12-01 Thread Xavier Noria

On Dec 1, 2005, at 15:13, SG Edwards wrote:


Hi folks,

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;


If you just want to count occurrences you can use a counter:

my $c = 0;
++$c while $line =~ /sentence/g;

There's a shorter idiom that takes advantage of the way list  
assignments work in scalar context that involves something known as  
the "goatse operator" (that's jargon it's not a real operator):


my $c =()= $line =~ /sentence/g;

but you have to choose which one is more easy to understand for you.

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


I don't know whether this is what you ask for, but m//g in list  
context returns the captures in one shot:


my @tags = $html =~ /<(\w+)/g;

That is documented in perlop.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Count number of times matched

2005-12-01 Thread Jeff 'japhy' Pinyan

On Dec 1, SG Edwards said:

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!



$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.


No, you haven't.  A pattern match in scalar context only matches ONCE. 
What you want to do is:


  my @matches = $line =~ /($pattern)/ig;

Assigning the return value of the pattern match to an array means that the 
pattern match is executed in LIST context (instead of scalar context), so 
it will match as many times as it can (due to the /g modifier).


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Count number of times matched

2005-12-01 Thread Shawn Corey

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.0002 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]
 




Count number of times matched

2005-12-01 Thread SG Edwards

Hi folks,

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?)


Sorry for the long-winded question, any help appreciated!




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]