open (file)
if (line starts with $reference) {
delete entire line
}
close (file)
Excuse my hastily written psuedo-code but could anyone help me convert this to perl? I can delete a line based on the line number, or the last line of the file no problem but I can't find anywhere that tells me how to delete a line based on what it starts with! I imagine it involves reading the contents of the file into an array (or variable?) and then using a regular expression on it, if that matches then delete, only problem is I don't know how to do it!
Any help is greately appreciated.
Cheers.
Si.
$pr = 1;
open(IN, "$file") ¦¦ die('Cannot open inputfile');
open(OUT, ">$tempFile") ¦¦ die('Cannot open outputfile');
while(<IN>) {
if (/<tr><td>$entry</) {
$pr = 0; # turn off printing if the right line
}
else {
$pr = 1; # turn it on again if it's the start of another entry
}
print OUT if ($pr eq "1");
}
close(OUT);
close(IN);
rename($file, $newname) ¦¦ warn "Couldn't rename $tempFile to $file: $!\n";
print "Record has been deleted<br />";
The record contains I'm trying to delete will contain the table row and cel tags followed by a number, thats the trigger to delete the line. I've checked all the usual stuff like file permissions and locations but like I said, I'm brand new to this Perl lark lol
Cheers :)
[perl]
open IN, "< $file" or die "Cannot open $file: $!";
open OUT, "> $tempFile" or die "Cannot open $tempFile: $!";
while (<IN>) {
unless (/<tr><td>\Q$entry\E</) {
print OUT $_;
}
}
close OUT;
close IN;
unlink $file or die "Cannot unlink $file: $!";
rename $tempFile, $file or die "Cannot rename $tempFile to $file: $!";
print "Record has been deleted<br />";
[/perl]
This is the code I'm now using:
open (IN, "< $file") or die "Cannot open $file: $!";
open (OUT, "> $tempFile") or die "Cannot open $tempFile: $!";
while (<IN>) {
unless (/^<tr><td>\Q$entry\E</) {
print OUT $_;
}
}
close OUT;
close IN;
unlink $file or die "Cannot unlink $file: $!";
rename $tempFile, $file or die "Cannot rename $tempFile to $file: $!";
print "Record $entry has been deleted.<br />"; I'm printing out the variable $entry just to make sute its being taken in, which it is. Any ideas?
Cheers.