I'm trying to remove a line from a flat data file with form input. The file is a list of URLs.
The file is similar to this
I tried the code below to delete a specific link
sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");
}
and it deletes the entry, but leaves a blank line in its place. It leaves a data file like this
I then added to the subroutine as follows, trying to get rid of the blank line
sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");
open(READFILE,"<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
while(<READFILE>){
next if $line eq '';
push (@lines,$_);
}
close READFILE;
my $lines = join(/\n/,@lines);
open(OUT,">$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
print OUT $lines;
close OUT;
}
This code deleted all of the lines, whether blank or not.
I'm at a loss...any help would be appreciated.
The above file should look like this after deletion.
Thanks, Bob
The file is similar to this
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link5.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com
I tried the code below to delete a specific link
sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");
}
and it deletes the entry, but leaves a blank line in its place. It leaves a data file like this
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com
I then added to the subroutine as follows, trying to get rid of the blank line
sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");
open(READFILE,"<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
while(<READFILE>){
next if $line eq '';
push (@lines,$_);
}
close READFILE;
my $lines = join(/\n/,@lines);
open(OUT,">$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
print OUT $lines;
close OUT;
}
This code deleted all of the lines, whether blank or not.
I'm at a loss...any help would be appreciated.
The above file should look like this after deletion.
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com
Thanks, Bob