Note: Perl version 5.10+ is now needed for both the  \K operator and the  ++ possessive quantifier:

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:  <%-{-{-{-<


In reply to Re^3: Count and print in perl by AnomalousMonk
in thread Count and print in perl by kanikas16

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.