in reply to Count and print in perl

If you wish to use unpack, you need one template for lines 1-9 and another for lines 10-99.
use strict; use warnings; use List::MoreUtils qw(natatime); my $string = '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 ' . '4 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 ' . '5 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '6 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '7 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 ' . '8 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '9 1 1 0 0 1 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 ' . '12 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 ' . '13 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0' ; my $template1 = 'A2' x 18; my $template10 = 'A3' . 'A2' x 17; my $it = natatime 18, unpack( $template1 x 9 . "($template10)".'*' , $ +string); while (my @vals = $it->()) { printf '%2d' x 18 . "\n", @vals } OUTPUT: 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 4 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 5 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 6 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 7 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 8 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 9 1 1 0 0 1 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 12 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 13 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0
Bill

Replies are listed 'Best First'.
Re^2: Count and print in perl
by kanikas16 (Initiate) on Apr 20, 2017 at 06:49 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 <my $template1 = 'A2' x 18; my $template10 = 'A3' . 'A2' x 17; my $it = natatime 18, unpack( $template1 x 9 . "($template10)".'*' , $ > ,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.
      Given only what you have told us so far, that task is impossible! We need a specification (not just an example) of your input 'file' format. Your original post implied that your file is an ASCII (not 'binary') file of fixed length records without separators. The only way to tell the end of a line was by its length. That is definitely a job for unpack. Note: I intentionally wrote the input in my example in a way that made every character (especially whitespace) explicit. If your lines are separated by an INPUT_RECORD_SEPARATOR (e.g. newline) unpack is probably overkill. Without a specification, we have to guess how to parse your file. That has not been going well for you.
      Bill