in reply to Re: Count and print in perl
in thread Count and print in perl

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.

Replies are listed 'Best First'.
Re^3: Count and print in perl
by AnomalousMonk (Archbishop) on Apr 20, 2017 at 14:54 UTC

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