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:
- code
- verbatim error and/or warning messages
- a coherent explanation of what "doesn't work actually means.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|