Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Pattern Finding

by jackdied (Monk)
on Sep 12, 2001 at 21:06 UTC ( #111955=note: print w/ replies, xml ) Need Help??


in reply to Pattern Finding

This little guy will do want you want.
The string can't have spaces, or you'll have to use a
different seperator rather than a space. If you wanted it
to be bullet proof, you would have to use arrays and do the
regexp compares on every element.

WARNING: If there is a pattern that is never repeated, this will go into an infinite loop

It is not commented, but short enough to be self explanitory (?)

#!/usr/bin/perl
use strict;
use Data::Dumper;

my $orig_str = 'helloworldhellohellohihellohiworld';

my @patterns = ();

my $str = $orig_str;

while (length($str)) {
    my $len = 1;
    my $token = substr($str, 0, $len);
    my $test_p = substr($str, $len);
    while ($test_p =~ /$token/) {
        $len++;
        $token = substr($str, 0, $len);
        $test_p = substr($str, $len);
    }
    $token = substr($str, 0, $len - 1);
    push @patterns, $token;
    $token =~ s/ //g;
    $str =~ s/$token/ /g;
    $str =~ s/^ *//;
}

print Dumper(\@patterns);


Comment on Re: Pattern Finding

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-10-23 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls