Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Capture variable length pattern when it repeats variable times

by exilepanda (Monk)
on Apr 02, 2013 at 05:10 UTC ( #1026581=perlquestion: print w/ replies, xml ) Need Help??
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;

Comment on Capture variable length pattern when it repeats variable times
Select or Download Code
Re: Capture variable length pattern when it repeats variable times
by BrowserUk (Pope) on Apr 02, 2013 at 05:20 UTC

    You'll have to benchmark to see if this is more efficient:

    my %uniq; my $s = "ABCABCABC1231234123123ABCABCABC123TestTestTestTestTest1234123 +123123121212121212121212121ppppp";; print for grep{ ++$uniq{$_} == 1 } $s =~ m[(?=(.+)\1{4})]g;; Test 1212 2121 12 21 p

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Work like a charm!! Big Thanks!!

      I added $String .= $String for ( 1..4 ) and run the test. Yours cost 0sec while mine took 6secs. Totally be defeated.

      This also reminds me that mastering regex is really another great topic, though I can read and understand your code, but believe thaz quite far to figure out one like this by myself! Thank you very much again! =)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026581]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-07-30 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls