> Hi all, Howdy
> 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"); Ok, If I were tryign that and assuming the file is a relatively reasonable size ascci file.... 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]