http://qs321.pair.com?node_id=1188220


in reply to Count and print in perl

TIMTOWTDI -- without resorting to sprintf or pack ...

and with humble bows to johngg for explicitly questioning OP's use of "binary file" (which the example ain't) and to BillKSmith for addressing the need for multiple templates, depending on how many digits occur in the LineNumers!

#!/usr/bin/perl -w use strict; # 1188105 my @data = qw(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 $linemarker = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; my $item; my $seen = '0'; print "\n(\n"; # op wants to start (& end) the printout with a par +en, # but -- in practice -- if the array is read fro +m # a "binary file," AS STATED, there won't be any pa +rens # in the array for $item(@data) { if ( $item !~ /\d+/ ) { $item = ord($item); # xlate char to num } elsif ( ($item == 1) && ($seen =~ /0/) ) { # first array eleme +nt? # Then it's a linen +umber (LN) print "$item "; # so print LN with +spacing $seen = "seen"; # set flag so future number 1s won't +be taken for LNs next; # and move on to next array element } elsif ( (10 > $item) && ($item > 1) && ($seen =~ /seen/) ) { # HACK to line up t +he elements print "\n$linemarker\n$item "; next; } elsif ( (100 > $item) && ($item > 9) && ($seen =~ /seen/) ) { print "\n$linemarker\n$item "; # if LN > 9 and < 9 +9, # print fewer space +s -- end HACK } else { print "$item "; } } print "\n)\n";

OUTPUT of 1188105.pl

( 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 3 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 4 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 5 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 6 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 7 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 8 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 9 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 10 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 11 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 12 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 13 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 )

Update: Missed credit to 1nickt for sprintf templating. My bad! ++ to each.


Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
  1. code
  2. verbatim error and/or warning messages
  3. a coherent explanation of what "doesn't work actually means.