c:\@Work\Perl\monks\kanikas16>perl -wMstrict -le
"use 5.010;
;;
my $s =
join(' ', map { $_, map { int rand 2 } 1 .. (10 + rand 10) } 1 .. 1
+3)
;
$s = qq{($s)};
print qq{'$s' wrap-around! \n};
;;
my $bar = qq{\n} . 'x' x 35 . qq{\n};
;;
$s =~ s{ \A [(] \K }{\n}xms;
$s =~ s{ (?: \G | (?<= \n)) \d+ (?: \s+ [01] \b)++ \K \s+ }{$bar}xmsg
+;
print qq{'$s'};
"
'(1 0 0 1 1 0 1 1 1 1 0 0 2 0 0 1 0 1 0 1 0 0 1 1 3 0 0 1 1 1 1 1 1 1
+0 0 0 0 1
1 4 0 0 0 0 1 1 0 1 1 1 0 1 5 1 1 1 0 0 0 0 0 0 1 6 1 0 0 1 1 0 0 1 0
+1 1 0 1 0
7 0 1 0 1 1 1 1 1 1 1 0 0 1 0 0 8 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 0 1 1
+9 1 0 1 0
0 1 0 0 1 1 10 1 0 0 1 1 0 1 0 0 1 1 0 11 1 1 0 0 0 0 0 1 0 1 12 0 1 0
+ 0 1 1 0 0
0 1 0 1 0 1 0 1 1 1 13 1 0 1 1 1 0 1 0 1 0 0 0 1 0)' wrap-around!
'(
1 0 0 1 1 0 1 1 1 1 0 0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2 0 0 1 0 1 0 1 0 0 1 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4 0 0 0 0 1 1 0 1 1 1 0 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
5 1 1 1 0 0 0 0 0 0 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6 1 0 0 1 1 0 0 1 0 1 1 0 1 0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
7 0 1 0 1 1 1 1 1 1 1 0 0 1 0 0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 0 1 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9 1 0 1 0 0 1 0 0 1 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10 1 0 0 1 1 0 1 0 0 1 1 0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
11 1 1 0 0 0 0 0 1 0 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12 0 1 0 0 1 1 0 0 0 1 0 1 0 1 0 1 1 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
13 1 0 1 1 1 0 1 0 1 0 0 0 1 0)'
Update: If the number of 0/1s in each numbered group cannot be known before processing, a regex approach might actually be best. If this number is known, one of the other approaches in this thread might be better. This is especially true if the pesky ( ) parentheses can be clipped off of the string before processing and added back, if necessary, afterward. I'm attracted to regexes as solutions for problems like this because of their puzzle-like nature.
Update 2: The Anonymous Monk's \b (?= \d) (?! [01] \b) here is simpler, with the same effect. Nice.
Give a man a fish: <%-{-{-{-<
|