I tried your suggestion and got the following output:
1) the first col didn't print, and the 3rd col overwrote the 2nd; this
is the main stumbling block
2) also, what if example.txt has 36 lines with the same format as
described.
FYI I'm using cgywin's version of perl.
--
On Apr 26, 9:55=A0am, rob.di...@[EMAIL PROTECTED]
(Rob Dixon) wrote:
> evan9...@[EMAIL PROTECTED]
wrote:
> > The following script is to read 4 consecutive lines at a time from a
> > file, concatenate the first 3 lines
> > (with a ", "), and print the result to STDOUT. =A0If the 3 lines
aren't
> > concatenated they print correctly, however
> > if they are, the result is gibberish. =A0Any suggestions. =A0 thx.,
EC.
> >
------------------------------------------------------------------------=
---=AD----------------------------------------------------------------------=
-
> > #!/bin/perl
> > # =A0Read a series of 4 rows from a file and print the first 3 on
> > # =A0the same line.
>
> > $file =3D 'example.txt'; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Name the
file
> > open(INFO, $file); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# Open
the=
file
> > $row_num =3D 0;
> > while (<INFO>) {
> > =A0 =A0 $i =3D $row_num%4;
> > =A0 =A0 if ($i <=3D 2) {
> > =A0 =A0 =A0 =A0 $col[$i] =3D "$_";
> > =A0 =A0 }
> > =A0 =A0 if ($i <=3D 1) {
> > =A0 =A0 =A0 =A0 chomp ($col[$i]);
> > =A0 =A0 }
> > =A0 =A0 if ($i =3D=3D 2) {
> > =A0 =A0 =A0 =A0 #$row =3Djoin(', ', @[EMAIL PROTECTED]
);
> > =A0 =A0 =A0 =A0 printf ("%s", $col[0]);
> > =A0 =A0 =A0 =A0 printf (", ");
> > =A0 =A0 =A0 =A0 printf "%s, ", $col[1];
> > =A0 =A0 =A0 =A0 printf "%s\n", $col[2];
> > =A0 =A0 }
> > =A0 =A0 $row_num++;
> > }
> > close(INFO); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#
Cl=
ose the file
>
> Something like this is, I believe, easier both to code and to
understand.
>
> HTH,
>
> Rob
>
> use strict;
> use warnings;
>
> use constant FILE =3D> 'example.txt';
>
> open INFO, FILE or die $!;
>
> my @[EMAIL PROTECTED]
>
> while (<INFO>) {
> =A0 chomp;
> =A0 push @[EMAIL PROTECTED]
$_;
> =A0 last if @[EMAIL PROTECTED]
=3D=3D 3;
>
> }
>
> printjoin(', ', @[EMAIL PROTECTED]
), "\n";- Hide quoted text -
>
> - Show quoted text -


|