in reply to counting overlapping patterns
#!/usr/bin/perl use strict; use warnings; print str_count('AAAA', 'AA'), "\n"; sub str_count { my ($str, $pat) = @_; my $tot; for ( 0 .. length( $str ) - length( $pat ) ) { $tot++ if index($str, $pat, $_) - $_ == 0;; } return $tot; }
Cheers - L~R
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: counting overlapping patterns
by Limbic~Region (Chancellor) on Feb 19, 2005 at 16:14 UTC |