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 am, [EMAIL PROTECTED] (Rob Dixon) wrote: > [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. If the 3 lines aren't > > concatenated they print correctly, however > > if they are, the result is gibberish. Any suggestions. thx., EC. > > -------------------------------------------------------------------------------------------------------------------------------------------------- > > #!/bin/perl > > # Read a series of 4 rows from a file and print the first 3 on > > # the same line. > > > $file = 'example.txt'; # Name the file > > open(INFO, $file); # Open the file > > $row_num = 0; > > while (<INFO>) { > > $i = $row_num%4; > > if ($i <= 2) { > > $col[$i] = "$_"; > > } > > if ($i <= 1) { > > chomp ($col[$i]); > > } > > if ($i == 2) { > > #$row =join(', ', @col); > > printf ("%s", $col[0]); > > printf (", "); > > printf "%s, ", $col[1]; > > printf "%s\n", $col[2]; > > } > > $row_num++; > > } > > close(INFO); # Close the file > > Something like this is, I believe, easier both to code and to understand. > > HTH, > > Rob > > use strict; > use warnings; > > use constant FILE => 'example.txt'; > > open INFO, FILE or die $!; > > my @col; > > while (<INFO>) { > chomp; > push @col, $_; > last if @col == 3; > > } > > printjoin(', ', @col), "\n";- Hide quoted text - > > - Show quoted text - -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/