Forum Moderators: coopster & phranque

Message Too Old, No Replies

Deleting lines in an external file

         

sjthomas

12:50 pm on Oct 26, 2004 (gmt 0)

10+ Year Member



I'm a bit new to the whole Perl/CGI thing and I'm having a bit of a problem. I've got a script that takes user entries and stores them in a file. It stores a reference number as the first line and every entry is on one line only. Basically I want to be able to delete lines from that file based on the reference number. In my head it goes somehting like this:

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.

moltar

1:29 pm on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is your field delimiter?

sjthomas

6:16 pm on Oct 26, 2004 (gmt 0)

10+ Year Member



This is the copy of the code I'm using:

$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 :)

moltar

4:58 am on Oct 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have not tested this, but it should work:

[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]

sjthomas

11:41 am on Oct 27, 2004 (gmt 0)

10+ Year Member



Thanks for that, the only problem is that it now deletes every record not just the one with the unique id. Any ideas? Also can I ask what the Q and the E does around the variable? Is that a way of telling the regexp that there is a variable in there? I assumed you had to do that and I had tried quotes before but with no success. Thanks for all your help!

sjthomas

8:39 pm on Oct 27, 2004 (gmt 0)

10+ Year Member



Actually it doesn't delete every record. It will only delete every record if you try and delete the first one. If you try and delete any of the others it does nothing!

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.

sjthomas

10:47 pm on Oct 27, 2004 (gmt 0)

10+ Year Member



OK I feel the need to hold my hands up and admit to the fact that I'm an idiot on this one. I got the script working, basically I had missed a /n out of the script and that was messing it up. D'oh!

Thanks for all your help!