in reply to printing specific lines

i was perhaps thinking
open (OUT, ">gout"); while (<>){ chomp; $line = $_; push @array, $line; } print @array; open (IN, "<grep"); while (<IN>){ next unless $. == $array[$i++]; print OUT $_; #if ($. == @array) {print OUT $_} }

Replies are listed 'Best First'.
Re^2: printing specific lines
by ambrus (Abbot) on Feb 27, 2005 at 22:54 UTC

    This code has a well-hidden bug (or maybe I'm too tired): you always increment $i even if the equalty doesn't stand. Change

    next unless $. == $array[$i++];
    to
    next unless $. == $array[$i]; $i++;
    and it should work.

    Also, you should really add or die "open: $! to your open statement.

    Update: and to eliminate a warning, you may want to replace

    next unless $. == $array[$i++]; print OUT $_;
    to
    next unless $. == $array[$i]; print $_; ++$i < @array or last;