http://www.perlmonks.org?node_id=212796

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

Given a string consisting of runs of chars, eg.

0 1 2 3 4 5 + 6 01234567890123456789012345678901234567890123456789012345 +67890 my $string =' aaaa bbbbccccccccbbbb aaaabbbbbcdddddddddddddddd +dddd';

I need to iterate over the string returning, (value, startpos and length) after each iteration. So the values for the above string would be

(' ',0,4), ('a',4,4),(' ',8,4),('b',12,4),('c',16,8),('b',24,4),(' ',28,3),('a',31,4),('b',40,1)...

There is no significance in the choice of example string, each run could be of any char 0..255. Nor the lengths, the number of 4's and multiples thereof seems to be something to do with the repeat speed of my keyboard and my reaction time.

I don't really mind if that means I get an array with the values interleaved, 3 arrays with the triples spanning the arrays.

I've tried various methods, but I feel that a clever regex solution using \G and /cg in a loop would be possible and probably more efficient that most I've tried, but it escapes me. NOTE: My goal here is absolutely efficiency not keystroke golf.

I'll benchmark any and all solutions offered, what I'm really after is the regex solution if its possible and also alternative methods.

Thanks, BrowserUK


Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.