It's unclear what "newfile" should contain. If you want it to have
one 111 ury
th ese ll
in it, thenyour program makes absolutely no sense. Since this smells like homework, here are some questions I am asking myself, and then a proposed solution that the prof probably won't accept ;) :
- Why are you counting occurances or existance of the lines in oldfile? How does this advance your aims to delete a line?
- Why are you printing the number of times oldfile has a certain line to the standard output. This makes little sense to me.
And, to wrap it up, a solution for inplace editing (this is as per what you seem to be asking.):
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my $value_to_delete = <STDIN>;
chomp $value_to_delete;
tie my @file, 'Tie::File', "file" or die "Couldn't open: $!";
@file = grep { $_ ne $value_to_delete } @file;
untie @file;
N.B. This solution is quite cost effective memory-wise, because Tie::File does some cool behind-the-scenes stuff so you don't have to read everything in at once. This is huge if the file is very large.
UPDATE: Here's a non-in place solution:
#!/usr/bin/perl -pi.orig
BEGIN {$value_to_delete = <STDIN>}
undef $_ if $_ eq $value_to_delete;