in reply to how to count the number of repeats in a string (really!)
Which gives a result that slightly differs of your expected values, but I believe is a accurate:use strict; use warnings; my $str = 'aabcdabcabcecdecd'; my $min_length = 1; my $min_count = 2; my %count; count( $str ); while (my ($string, $counts) = each %count) { if ($counts >= $min_count) { print "$string => $counts\n" if length($string) >= $min_length; } } exit; sub count { my( $string) = @_; my $length = length( $string ); return if $length < $min_length; for my $l ($min_length..$length) { my $substring = substr( $string, 0, $l ); $count{$substring} += 1; } count( substr( $string, 1 ) ); }
and respectivelyec => 2 ecd => 2 ab => 3 abc => 3 bc => 3 cd => 3
e => 2 a => 4 ecd => 2 ab => 3 c => 5 abc => 3 bc => 3 cd => 3 d => 3 ec => 2 b => 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to count the number of repeats in a string (really!)
by blazar (Canon) on Nov 14, 2007 at 21:08 UTC |