http://www.perlmonks.org?node_id=1026581

exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

In this task, I want to capture any length pattern that continually repeated $NeedRepeat times to @res. My code works, but looks really expensive. Any better approach for this ?

use strict; # start config my $NeedRepeat = 5 ; my $String = "ABCABCABC1231234123123ABCABCABC123TestTestTestTestTest12 +34123123123121212121212121212121ppppp"; # end config my $strMaxLen = length ( $String ); my (@res,%temp); foreach my $startPos ( 0 .. $strMaxLen ) { foreach my $getLen ( 0 .. $strMaxLen ) { $String =~ /.{\Q$startPos\E}(.{\Q$getLen\E})/; my $targetPhrase = $1; next unless $targetPhrase; $temp{$1} = 1 if ( $String =~ /(\Q$targetPhrase\E){\Q$NeedRepe +at\E}/ ) ; } } @res = sort keys %temp; print $_ . $/ foreach @res;