evan9021@[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.
I tried your program and I don't see where the problem lies? If you
could show what the output is supposed to look like and what the actual
output looks like?
>
--------------------------------------------------------------------------------------------------------------------------------------------------
> #!/bin/perl
use warnings;
use strict;
> # Read a series of 4 rows from a file and print the first 3 on
> # the same line.
>
> $file = 'example.txt'; # Name the file
my $file = 'example.txt';
> open(INFO, $file); # Open the file
You should *always* verify that the file opened correctly:
open INFO, '<', $file or die "Cannot open '$file' $!";
> $row_num = 0;
> while (<INFO>) {
> $i = $row_num%4;
Why not just use Perl's built-in $. variable?
perldoc perlvar
> if ($i <= 2) {
> $col[$i] = "$_";
> }
> if ($i <= 1) {
> chomp ($col[$i]);
> }
> if ($i == 2) {
> #$row = join (', ', @[EMAIL PROTECTED]
);
> printf ("%s", $col[0]);
> printf (", ");
> printf "%s, ", $col[1];
> printf "%s\n", $col[2];
You don't really need printf there:
print "$col[0]";
print ", ";
print "$col[1], ";
print "$col[2]\n";
Or simply:
print "$col[0], $col[1], $col[2]\n";
> }
> $row_num++;
> }
> close(INFO); # Close the file
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall


|