++ed by:

1 non-PAUSE user.

Author image Janek Schleicher


String::Substrings - module to extract some/all substrings from a string


  use String::Substrings;
  my @parts  = substrings $string;
  my @tripel = substrings $string, 3;


This module has only one method substrings. It is called as

  substrings STRING [,LENGTH]

Without a length specification, tt returns all substrings with a length of 1 or greater including the string itselfs. The substrings returned are sorted for the length (starting with length 1) and for their index. E.g. substrings "abc" returns ("a","b","c","ab","bc","abc"). This order is guaranteed to stay even in future versions. That also includes that the returned list of substrings needn't be unique. E.g. substrings "aaa" returns ("a","a","a","aa","aa","aaa").

With a length specification, it returns only substrings of this length. This notion is equivalent to grep {length($_) == $length} substrings $string.

substrings "" returns an empty list, substrings undef returns undef and every call with a hash/array-reference let substrings die.

In scalar context it returns the number of substrings found, allthough I can't imagine that it is useful. (It's simple to calculate without determining all the substrings: length($string) * (length($string)+1) / 2. Especially the scalar context behavior could be changed in future versions.

Please take care to the length of the strings passed. The number of substrings grows with the square of the string's length. I only tested it till a string length of 100.






Janek Schleicher, <bigj@kamelfreund.de>


Copyright 2002 by Janek Schleicher

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.