in reply to Re^4: 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?

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.

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

Replies are listed 'Best First'.
Re^6: How can I count characters between two same substrings in a string where the substring is repeated more than 5 times?
by ww (Archbishop) on Dec 11, 2011 at 00:51 UTC
    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.