in reply to group things?

Please correct me if my assumptions are wrong: Basically, what you want is to find the start and end position of subsequences of same characters that aren't "-".

A straightforward way to do that is to inspect each character in sequence and compare it to the previous one. But that would be slow and annoying. It's easier to use a regex:

#!/usr/bin/perl -w use strict; $_ = "-----MMMMM------IIIII----MMM---OOOO---I---MMMM-----"; while (/(\w)(\1*)/g) { print "$1:".(1+pos()-length "$1$2")."-".pos."\n"; }

Replies are listed 'Best First'.
Re^2: group things?
by Anonymous Monk on Apr 25, 2008 at 22:30 UTC
    Joost, your code works perfect for me... Unfortunately, I don't seem to understand much of what you do... but thank you for your time...
      As someone else already hinted, you would probably benefit from reading perlretut and pos.

      It may seem like overkill for this problem but learning regular expressions will make string processing problems like this a lot easier. And almost all serious programming languages today have a regular expression library very much like perl's, so chances are high you'd be able to use that knowledge if you're using some other language too.

        Oh, backreferences were the catch then? I saw about it in the tutorial... Thank you guys, both of you!
      OK, Joost's code works, now it's time for you to go to work.

      The code uses only a regex and two built-in functions, pos and length, and string concatenation (the . operator). Time to apply Your Mother's suggestions about documentation access and do some reading. Then, maybe try some variations and see what happens. After that, specific questions will usually elicit pertinent answers.