Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!
I have come up with a string in a program of mine, that has the following look:
-XXX--XXXXXXXXX...XXXXXXWW--WWW.WWWZZZ---ZZZWW..W-.-WWWWWXXX
I need to have same letters printed together, ignoring - and . (both - and . should be treated relevant to what letters are next to them).
So, the output would be:
1-24:X 25-35:W 36-44:Z 45-57:W 58-60:X

Replies are listed 'Best First'.
Re: How to deal with such strings?
by Corion (Patriarch) on Jun 10, 2009 at 08:23 UTC

    You haven't shown what you've already tried. The following one-liner produces the output:

    >perl -lne "$l=1;while (/([-.]*([\w])\2*[-.]*(\2+[-.]*)*)/g){ print sp +rintf '%d-%d:s',$l,pos,$2;$l=pos()+1}"

    See perlre for the regular expression.

    You might want to specify what output should be produced for:

    XXX---YYY
Re: How to deal with such strings?
by almut (Canon) on Jun 10, 2009 at 08:21 UTC
    both - and . should be treated relevant to what letters are next to them

    In case you have dashes/dots in between, such as ...WWW--ZZZ..., would the "--" belong to the previous or next letter group? Or does this not happen?

Re: How to deal with such strings?
by ikegami (Patriarch) on Jun 10, 2009 at 13:35 UTC

    @-, @+

    $_ = '-XXX--XXXXXXXXX...XXXXXXWW--WWW.WWWZZZ---ZZZWW..W-.-WWWWWXXX'; printf "%s-%s: %s\n", $-[0]+1, $+[0], $1 while /[.-]*([^.-])(?:\1|[.-])*/g;
    1-24: X 25-35: W 36-44: Z 45-57: W 58-60: X

    Update: Bug fix. (Wasn't including leading "-" and "." characters.)

Re: How to deal with such strings?
by JavaFan (Canon) on Jun 10, 2009 at 08:23 UTC
    So, what's the output if the string is "X-Y"? Does the dash belong to the X or the Y? What if the string is "-.-.-"?
Re: How to deal with such strings?
by Anonymous Monk on Jun 10, 2009 at 08:34 UTC
    Hi guys, thank you all!
    I don't think there will be a case of XXX----YYY (based on what I've seen from the output so far) but, if it happens, the dashes/dots will belong to the previous letter (i.e. X in this example)...