> I'm still having real trouble deleting a line from a file. I've read the
> article on Perldoc, but it doesn't help me. Does anyone know how to do it?
what part of the answer is giving you trouble?
--
brian d foy <com...@panix.com>
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html
a simple way...
open file
read the file into an array.
delete the line you want removed
shuffle rest of file up to fill empty array element
write array to file (not append but overwrite)
close file
done.
how about something like:
system("cp $file $file.old");
open(OLD, "$file.old");
open(NEW, ">$file");
while (<OLD>) {
print NEW unless (/$pattern/ || $. == 0); # delete matching line, if
# any, and first line
};
close(OLD);
close(NEW);
you may wish to edit the file in place. see the perl cookbook
for an answer.
sub delete_line{
($line,$source,$dest)=@_;
open (FILE,"<$source") || die $!;
open (FILE2,">$dest") || die $!;
while (<FILE>){
$i++;
(print FILE2 $_) unless ($i==$line);
}
close (FILE);
close (FILE2);
}
hm, why is this an improvement over what i posted?