[comp.lang.perl] extra line mystery

m1dib00@fed.frb.gov (Douglas I. Battenberg) (07/05/90)

The following script reads a file, edits some lines, and prints the whole
thing back into a new file. The problem is the output file ends up with an
extra blank line at the end.  If I count lines and print the array with a
for loop, I get the correct number of lines. I've monitored this with the
debugger but cannot see where the extra line is coming from. What am I
missing here?

#! /usr/bin/perl

$\="\n";   #  set automatic newline for print statements
$| = 1;    #  clear buffers
$* = 1;    #  do multi string matches

;#  initialize arrays

@model_cards = ();
@judge_cards = ();

open(modelcc,"/mq/mps/qmod/data/cards/model");

chop(@model_cards = <modelcc>);  # read model cards into an array
@judge_cards = @model_cards;

;#  create judge cards from model cards

$lines = $#judge_cards;
$last_line = $judge_cards[$#judge_cards];

for ($i = 0; $i <= $#judge_cards; $i++) { 

  $_ = $judge_cards[$i];

  s|actfit/model|actfit/judge|ig;

  $judge_cards[$i] = $_;

}

open(judgecc,">/mq/mps/qmod/data/cards/judge");

print(judgecc @judge_cards);

close(modelcc);
close(judgecc);

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/06/90)

In article <M1DIB00.90Jul5161800@mqws8.fed.frb.gov> m1dib00@fed.frb.gov (Douglas I. Battenberg) writes:
: The following script reads a file, edits some lines, and prints the whole
: thing back into a new file. The problem is the output file ends up with an
: extra blank line at the end.  If I count lines and print the array with a
: for loop, I get the correct number of lines. I've monitored this with the
: debugger but cannot see where the extra line is coming from. What am I
: missing here?
: 
: #! /usr/bin/perl
: 
: $\="\n";   #  set automatic newline for print statements

If you don't want a newline at the end, then you don't want to set $\ to 
a newline.  In general, avoid setting $\ (and $,), since they're mostly
there to emulate awk.

Larry