in reply to Count and print in perl

Rather that trying to shoehorn pack into a solution, it seems simpler to me to regex it (caution: needs Perl version 5.10+ for  \K operator):

c:\@Work\Perl\monks\kanikas16>perl -wMstrict -le "use 5.010; ;; my $s = '(1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ' . '2 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '3 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ' . '10 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '11 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 ' . '987 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0)' ; print qq{<<$s>> \n}; ;; my $bar = qq{\n} . 'x' x 35 . qq{\n}; $s =~ s{ \A \( \K }{\n}xms; $s =~ s{ \d+ \s+ (?: [01] \s+){17} \K }{$bar}xmsg; print qq{<<$s>>}; " <<(1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 + 0 1 3 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 10 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 11 + 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 987 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0)>> <<( 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 10 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 11 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 987 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0)>>
This is based on the updated version of kanikas16's OP and (almost!) exactly produces the desired output. The only thing I can't get simply via substitutions is the use of a 36-'x' separator after row 8 (per the OP) rather than the 35-'x' separator used everywhere else. I'm assuming (and hoping!) this is just a typo; it would be possible to accommodate something like this with a substitution, but messy! (Note: This solution needs Perl 5.10+ for the  \K operator, but a non-\K solution is fairly painless; let me know if it's needed.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Count and print in perl
by kanikas16 (Initiate) on Apr 20, 2017 at 06:52 UTC

    Hi, This is working for me but what if i change no of bits to (13 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0). i removed two bits ,then

    $s =~ s{ \d+ \s+ (?: [01] \s+){17} \K }{$bar}xmsg;

    then its no working for me . i need generlised code which work for every input given whatever it is 18 bits or 19 bits or 20 bits .it will produce Table in aligned format from 1 to 13 iteration.

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