in reply to Why are lines being skipped in output?

Remove the line
$_ =~ <test>;
You are reading 2 lines for each iteration of your while loop.
--
Andreas

Replies are listed 'Best First'.
Re^2: Why are lines being skipped in output?
by bohrme (Scribe) on Mar 19, 2008 at 13:35 UTC
    Would you explain that? I'm not sure why 2 lines are being read. Of course, I've never seen =~ used in such a way either...

      Each time Perl sees an <test> it reads another line from 'test'. There is no problem with using =~ in conjunction with your <test> construct...but it does cause another read from test. I'm not sure what you were intending or hoping the line $_ =~ <test> was supposed to do. Can you say what you were thinking?

      In case you're wondering, the other read occurs in your loop control line while(<test>).

      To keep it from reading twice but to still do what it looks like you're wanting to do you can use a construct like:

      while (<test>) [ chomp; s/(th)/TH/gi; print "$_\n"; } close (test);

      Several other monks that have answered your inquiry show this and I think it is the right answer. But since I'm not sure what you intended with the line $_ =~ <test>, I'm not positive that deleting the line really results in what you were intending.

      ack Albuquerque, NM
      C:\>perl -MO=Deparse,-p #!usr/bin/env perl open (test, "@ARGV"); while (<test>) { $_ =~ <test>; chomp; s/(th)/TH/gi; print "$_\n"; } close (test); ^Z open(test, "@ARGV"); while (defined(($_ = <test>))) { ($_ =~ <test>); chomp($_); s/(th)/TH/gi; print("$_\n"); } close(test); - syntax OK