in reply to Re^3: How can I count characters between two same substrings in a string where the substring is repeated more than 5 times?
in thread How can I count characters between two same substrings in a string where the substring is repeated more than 5 times?

That's all well and good, tobyink, but coding your proposed solution increases the complexity far beyond anything readily identifiable as "simple."

To avoid complexity, one must know in advance that the delimiter follows something else at the start of the string (or is followed by something at the end)... in which case, counting with a pencil, paper and strike-thrus may be just as effective...
or

If the topology of the string is unknown, then you must deal with the permutations...

  • Comment on Re^4: How can I count characters between two same substrings in a string where the substring is repeated more than 5 times?

Replies are listed 'Best First'.
Re^5: How can I count characters between two same substrings in a string where the substring is repeated more than 5 times?
by tobyink (Canon) on Dec 11, 2011 at 00:27 UTC

    Actually, if the delimiter occurs at the start of the string, then split returns an empty string as the first portion. If passed a LIMIT of -1, it will also do the same at the end of the string. So if you really don't care about the lengths of the "abc" and "xyz" portions, it's simply:

    use 5.010; my $string = "abcFOOdefghFOOiFOOjklmFOOnopqrFOOstuvFOOwxyz"; my @portions = split /FOO/, $string, -1; shift @portions; pop @portions; say "Lengths are:"; say $_ for map { sprintf("%d ('%s')", length, $_) } @portions;

    As I said, the reason I didn't include the code to ignore the first and last portion of the string in my initial example is because it seemed obvious how it could be done.

      Absolutely.

      But, using your code assumes/requires prior knowledge of the string's content to decide whether to include or exclude Lines 4 and 5. Absent that knowledge, you have to add code to determine the locations of the first and last delimiter.