Henry Todd wrote:

[snip]

> 
> This is how I'm counting the number of "cc" pairs at the moment ($cc is
> my counter variable):
> 
> $cc++ while $sequence =~ /cc/gi;
> 
> But this only matches the literal string "cc", so if, as it scans
> $sequence, it finds "cccc" it's only counting it once instead of three
> times.
>

are you sure it counts it once instead of two:

[panda]# perl -le '$cc++ while('cccc' =~ /cc/g); print $cc'
2
[panda]#

> 
> What pattern do I need to be looking for in the $sequence if I want to
> count *all* occurences of "cc" -- even if they overlap?
> 

all you need is a little look ahead:

#!/usr/bin/perl -w
use strict;

my $cc = 0;

while('cbccccbc' =~ /c(?=c)/g){
        $cc++;
}

print $cc,"\n";

__END__

prints:

3

which is what you want right?

david
-- 
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)

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