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

Re: how to count the number of repeats in a string (really!)

by ikegami (Patriarch)
on Nov 14, 2007 at 18:45 UTC ( [id://650820]=note: print w/replies, xml ) Need Help??


in reply to how to count the number of repeats in a string (really!)

The regex engine is quite adept at backtracking to find all inputs that satisfy a set of conditions. The shortest solution should be a regex one.

my $str = 'aabcdabcabcecdecd'; local our %counts; $str =~ / (.{2,}) # or (.+) (?(?{ !$counts{$1} })(?=.*\1)) (?{ ++$counts{$1} }) (?!) /x; use Data::Dumper; print Dumper \%counts;

Update:
(?(?{ !$counts{$1} })(?=.*\1))
might be more efficient as
(?> (?(?{ !$counts{$1} })(?=.*\1)) )

Update: The above doesn't work. $str='ababa' fails. My simpler version doesn't suffer from this bug.

Replies are listed 'Best First'.
Re^2: how to count the number of repeats in a string (really!)
by ikegami (Patriarch) on Nov 14, 2007 at 19:28 UTC

    I guess I didn't explain why mine works and why the OP's doesn't work with ".+". Compare

    while ($str =~ /($re)/g) { print("$1\n"); }
    use re 'eval'; $str =~ / ($re) (?{ print("$1\n"); }) (?!) /x;

    Looks similar? But for the same input, they produce different results.

    Input:

    my $str = 'aabcdabcabce'; my $re = qr/a[^a]*/;

    Output from /.../g:

    a abcd abc abce

    Output from /...(?{ save results })(?!)/:

    a abcd abc ab a abc ab a abce abc ab a

    /...(?{ save results })(?!)/ is key in finding all possible matches. It forces the regex to try everything to obtain a match.

      I guess I didn't explain why mine works and why the OP's doesn't work with ".+".

      I personally believe that blokhead already did. It was just a trivial mistake/overview on my part.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://650820]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-18 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found