in reply to how to count the number of repeats in a string (really!)
The first loop find the repetitions, the second count them. if you want to get only 2 or more char substring, change $x+1 to $x+2.$s = 'xxaabcdabcabcecdecdxx'; $min_len = 2; $min_rep = 2; while($s=~/(.{$min_len,})(?=.*?\1)/g) { for my $x (0..length $1) { @saw{ map {substr $1, $x, $_} $x+2..length $1 } = (); } pos($s) = pos($s) +1 -length $1; # fix } for (keys %saw) { my $saw{$_} =()= $s=~/\Q$_/g; delete $saw{$_} if $saw{$_}<$min_rep; } ____ a 4 ab 3 abc 3 bc 3 c 5 cd 3 d 3 e 2 ec 2 ecd 2 x 4 xx 2
Oha
Update: added regex quoting to the last re
Update: shorter and print in order of findings:
while($s=~/(\w\w+)(?=.*?\1)/g) { foreach $x (0..length $1) { map { $y = substr $1, $x, $_; $saw{$y}++ || do { local pos $s = 0; my $c=()= $s=~/\Q$y/g; print "$y => $c\n"; } } ($x+1..length $1) } pos($s) = pos($s) +1 -length $1; # fix }
Update: fix a bug in the above code, added a pos() relocation (see #fix)
|
|---|
| 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:25 UTC | |
by oha (Friar) on Nov 16, 2007 at 14:58 UTC |