Ask a Question related to PERL Beginners, Design and Development.
-
G Lams #1
problem deleting record in a text file
Hi all,
I'm new to perl (that's why I'm here :-). I'm building a few cgis allowing
me to add, modify, delete users in a text file (perl version 5.8.0 on a
redhat 8 machine).
So far, I've been able to create the form and the perl scripts to add
users and verify their existence.
I'm now trying to delete one user/record inside my text file.
You will find below the script I wrote.
Actually the temporary file is created the way it should be but I didn't
succeed in renaming it to users.dat, saying "no such file or directory".
This should be not possible as the script succeeds in opening users.dat
and reading from it.
I know that a nicer solution is to use Tie::File but I have to say that
from the documentation I found I don't really understand how to do it :-(
Thanks in advance for any help
Gael
# value we will read from the html form
my $name=$ARGV[0];
$data_file="users.dat";
# we will read the database record by record, copy each recor to the
temporary file
# and simply forget to write the record to be deleted.
$tmp_file="users.tmp";
open(DAT, "<$data_file") || die ("Could not open users file!");
open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
while (<DAT>)
{
# extract the username (the first field) from the record
my ($username)=split(/\|/,$_);
# test the name agains the username
if ($name eq $username) {
# we've found the record to delete, so skip it and move to next record
next;
}
# write the original record out to the temporary file
print TMPDAT $_ or die "Error writing $tmp_file: $!\n";
}
close DAT or die ("closing the users file!\n");
close TMPDAT or die ("closing $temp_file: $!\n");
# we delete the old file
unlink $data_file or die ("Can't delete old $data_file: $!\n");
# and rename the new file to replace the old one.
rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
'$data_file': $!\n");
G Lams Guest
-
Deleting a Record using PHP and MySQL with DreamweaverMX 2004
Hi, I've followed along with the tutorial on Deleting a Record using PHP and MYSQL : ... -
deleting DNS record using dnscmd.exe
Hello, I want to delete a record "comwebcam.yahoo" from the zone "com" When typing - dnscmd localhost /recorddelete com comwebcam.yahoo A I... -
Deleting a Record.
Thanks for the Help on the other problems...all resolved. One last (hopefullly) problem I can't seem to get around for this program...why isn't... -
Deleting curent record
Hi all, can anyone tell me how to delete just the current record if tempdate >= todaydate then Set newMail=Server.CreateObject("CDONTS.newMail")... -
deleting access database record
I am trying to write an asp page to delete a record from my access database. I have written the page to display the info but when I select the entry... -
Dan Muey #2
RE: problem deleting record in a text file
Howdy> Hi all,
Ok, If I were tryign that and assuming the file is a relatively reasonable size ascci file....> I'm now trying to delete one user/record inside my text file.
> You will find below the script I wrote. Actually the
> temporary file is created the way it should be but I didn't
> succeed in renaming it to users.dat, saying "no such file or
> directory".
> This should be not possible as the script succeeds in opening
> users.dat
> and reading from it.
>
> I know that a nicer solution is to use Tie::File but I have
> to say that
> from the documentation I found I don't really understand how
> to do it :-(
>
> Thanks in advance for any help
>
> Gael
> # value we will read from the html form
> my $name=$ARGV[0];
>
> $data_file="users.dat";
>
> # we will read the database record by record, copy each recor to the
> temporary file
> # and simply forget to write the record to be deleted.
>
> $tmp_file="users.tmp";
>
> open(DAT, "<$data_file") || die ("Could not open users
> file!"); open(TMPDAT, ">$tmp_file") || die ("Could not open
> temporary file!");
>
> while (<DAT>)
> {
> # extract the username (the first field) from the record
> my ($username)=split(/\|/,$_);
>
> # test the name agains the username
> if ($name eq $username) {
>
> # we've found the record to delete, so skip it and move to
> next record next; } # write the original record out to the
> temporary file print TMPDAT $_ or die "Error writing
> $tmp_file: $!\n"; }
>
> close DAT or die ("closing the users file!\n");
> close TMPDAT or die ("closing $temp_file: $!\n");
>
> # we delete the old file
> unlink $data_file or die ("Can't delete old $data_file: $!\n");
>
> # and rename the new file to replace the old one.
> rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
> '$data_file': $!\n");
And assumign pipe delimted records where usernam is the first entry..
use File::Slurp;
#delete user's line form file
write_file('./users.dat', grep(!/^$u\|/, read_file('./users.dat')));
First write_file writes the out put of the grep function to the file.
Grep is taking an array of input from the read_file function and returning entries that don't begin with the contents of $u followed by a pipe. ( IE every entry except the user you're deleting or every line if there is no entry for that user)
By doing that it's one line, no temp files, and at worst case it will just write the original file back to the file unmodified so no loss there.
You can also use he grep method for verifying an entry or grabbinga single row, just fyi.
HTH
Dmuey
Dan Muey Guest
-
Rob Dixon #3
Re: problem deleting record in a text file
G Lams wrote:
Hi G.>
> I'm new to perl (that's why I'm here :-). I'm building a few cgis allowing
> me to add, modify, delete users in a text file (perl version 5.8.0 on a
> redhat 8 machine).
> So far, I've been able to create the form and the perl scripts to add
> users and verify their existence.
> I'm now trying to delete one user/record inside my text file.
> You will find below the script I wrote.
> Actually the temporary file is created the way it should be but I didn't
> succeed in renaming it to users.dat, saying "no such file or directory".
> This should be not possible as the script succeeds in opening users.dat
> and reading from it.
>
> I know that a nicer solution is to use Tie::File but I have to say that
> from the documentation I found I don't really understand how to do it :-(
>
> Thanks in advance for any help
>
> Gael
> # value we will read from the html form
> my $name=$ARGV[0];
>
> $data_file="users.dat";
>
> # we will read the database record by record, copy each recor to the
> temporary file
> # and simply forget to write the record to be deleted.
>
> $tmp_file="users.tmp";
>
> open(DAT, "<$data_file") || die ("Could not open users file!");
> open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
>
> while (<DAT>)
> {
> # extract the username (the first field) from the record
> my ($username)=split(/\|/,$_);
>
> # test the name agains the username
> if ($name eq $username) {
>
> # we've found the record to delete, so skip it and move to next record
> next;
> }
> # write the original record out to the temporary file
> print TMPDAT $_ or die "Error writing $tmp_file: $!\n";
> }
>
> close DAT or die ("closing the users file!\n");
> close TMPDAT or die ("closing $temp_file: $!\n");
>
> # we delete the old file
> unlink $data_file or die ("Can't delete old $data_file: $!\n");
>
> # and rename the new file to replace the old one.
> rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
> '$data_file': $!\n");
(Be nice to know your name?)
Your problem is mainly one of far too much junk in your source.
Comments should be present only if the code is obscure or devious
for a non-obvious reason. Look at this code (which I had to write
to understand what you were trying to do!)
use strict;
use warnings;
my $name = shift;
my $data_file = 'users.dat';
my $tmp_file = 'users.tmp';
open DAT, $data_file or die $!;
open TMPDAT, "> $tmp_file" or die $!;
while (<DAT>) {
my ($username) = split /\|/;
print TMPDAT $_ unless $name eq $username;
}
close TMPDAT or die $!;
close DAT or die $!;
unlink $data_file or die $!;
rename $temp_file, $data_file or die $!;
It may not be what you meant to write, but it's semantically
nearly identical (yes, except all that die text). But
use strict; # always
use warnings; # usually
gave me the error
Global symbol "$temp_file" requires explicit package name
showing straight away that your problem is that you've used
$tmp_file initially, and $temp_file later on. Now isn't that
easy?
BTW, ignore stuff like Tie::File and File::Slurp unless you need
them. I have never had an application that did.
Cheers,
Rob
Rob Dixon Guest
-
R. Joseph Newton #4
Re: problem deleting record in a text file
[email]g.lams@itcilo.it[/email] wrote:
....and you are getting your first opbject lesson:> Hi all,
>
> I'm new to perl (that's why I'm here :-).
use strict;
use warnings;
Note $tmp_file>
> $tmp_file="users.tmp";
>
> open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
There is no $temp_file.> rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
> '$data_file': $!\n");
Had you added the strict and warnings pragmas at the top of your file, you
would have been amply warned about the typo.
Joseph
R. Joseph Newton Guest



Reply With Quote

