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):
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.)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)>>
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 | |
by AnomalousMonk (Archbishop) on Apr 20, 2017 at 14:54 UTC |