in reply to removing lines from a file

Please use strict and warnings. Please do not use a filehandle called true, then have a while (<true>) that will go false. That hurt my head ;)

It is good practice to test the result of attempts to open a file, and polite to close them when you have finished reading from them.

perltidy is your friend

The three argument form of open is safer

update

Added a check that there is something remaining in the original file while editing
#!/usr/bin/perl use strict; use warnings; # true was a confusing name open ORIG, "<", "ORIGINAL" or die "probs: ORIGINAL : $!\n"; open EDIT, "<", "cut_out" or die "probs: cut_out2 : $!\n"; open REST, ">", "REST" or die "probs: REST : $!\n"; while (<EDIT>) { chomp; my $j = $_; print "grabbing $j lines to $.out.test\n"; open OUT, ">", "$.out.test" or die "can not open $.out.test : $!\n +"; while ($j--){ if (my $line = <ORIG>) { print OUT $line; } else { die "Ran out of input file before editing complete !\n"; } } close OUT; } close EDIT; while (<ORIG>) { print REST; } close ORIG; close REST; # rename ("REST", "COMPARE");

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: removing lines from a file
by Anonymous Monk on Feb 18, 2005 at 10:44 UTC
    thanks for that, works exactly the way i wanted cheers