On Tue, Oct 08, 2002 at 08:07:18AM -0600, Chris Staskewicz wrote:
> Is there an efficient method to find all "continuous" substrings of a
> string.  For example, in the word "green", I'd like to parse out:
> 
> g, r, e, e, n, gr, re, ee, en, gre, ree, and so on...

$ perl -MString::Substrings -e 'print join ", ", substrings "greenpeace"'

Since installing this module brings in most of CPAN's Test:: tree and
its myriad dependencies, here are the guts of it:

    my $strlength = length($string);
    my @s = ();
    if (defined $length) {
        return @s if $length == 0;
        push @s, map {substr $string, $_, $length} (0 ..  $strlength-$length);
    } else {
        foreach my $length (1 .. $strlength) {
            push @s, map {substr $string, $_, $length} 
                         (0 .. $strlength - $length);
        }
    }
    return @s;

So this is O(n^2)...

The manpage also suggests looking at Algorithm::ChooseSubsets

HTH, 
</lurk>,
Paul (who hasn't re-adjusted his filter since the list move :-)

-- 
Paul Makepeace ....................................... http://paulm.com/

"What is the square root of a duck? Death is the spoiler of all."
   -- http://paulm.com/toys/surrealism/
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to