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

Yes, I deviated deliberately for simplicity. If you want to ignore the leading and trailing sections, just shift the first element off the list you get back from split, then pop the last element off as well.
  • Comment on Re^3: 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^4: 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 10, 2011 at 16:39 UTC
    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...

    • Delimiter first in the string, and last
    • Delimiter NOT first in string, but is last
    • Delimiter first in string, but NOT last
    • Delimiter neither first nor last
    • Delimiter occurs an odd number of times in string where it is either first or last
    • ... (I'm sure I've missed some)

      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.