Others have answered the question about what the problem was, so this
is another suggestion. If the file isn't enormous, what about reading 
and writing all at once? It is more readable and could be more
efficient.

open INPUT, "<address.txt" or die "can't open address.txt: $!\n";
my @all_info = <INPUT>;
close INPUT;

chomp @all_info;

my $out = '';
while (@all_info) {
  $out .= join(',', splice @all_info, 0, 9)."\n";
}

open OUTPUT, ">addline.txt" or die "can't open addline.txt: $!\n";
print OUTPUT $out;
close OUTPUT;

Or, if there is a reason to access individual elements, to reorder
them, eliminate some, modify some before printing, whatever

# ...
while (@all_info) {

  my ($name, $company, $street, $city, $county, $state,
      $postalcode, $country, $phone) = splice @all_info, 0, 9;

  print OUTPUT "$company,$phone,$street,$city,$postalcode"\n;
}
close OUTPUT;

Katy

[EMAIL PROTECTED]


> Subject: How do I read groups of 9 lines from a file into 1 record?
>    Date: Fri, 19 Apr 2002 16:27:51 +0100
>    From: [EMAIL PROTECTED]
>      To: [EMAIL PROTECTED]
> 
> 
> 
> I have a file of names and addresses. Each name and address is over nine
> lines (including blanks). I want  to use this file in a word document as an
> address list.
> 
> My attempt so far has resulted in every line being printed nine times. Help
> please...
> 
> 
> #!/usr/contrib/bin/perl
> # open the input file
> open(INFILE,  '<address.txt');
> # open the output
> open(OUTFILE, '>addline.txt');
> # read the whole file
> #While there are lines in the infile
> #For each 9 lines print the lines, print a new line, reset counter
> while(<INFILE>)
>     {
>          $ThisLine = $_;
>          {    for ($i = 1; $i <= 9; $i +=1)
>                 {print OUTFILE  $ThisLine; print OUTFILE ","; next}
>         }
>          print OUTFILE "\n";
>          $i = 1;
>     }
> close(INFILE);
> close(OUTFILE);
> 
> 
> 
> 
> Cathy
> 
> Cathy Gear
> e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to