problem deleting record in a text file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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 : ...
    2. 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...
    3. 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...
    4. 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")...
    5. 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...
  3. #2

    Default RE: problem deleting record in a text file

    > 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
    Dan Muey Guest

  4. #3

    Default Re: problem deleting record in a text file

    G Lams wrote:
    >
    > 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");
    Hi G.

    (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

  5. #4

    Default Re: problem deleting record in a text file

    [email]g.lams@itcilo.it[/email] wrote:
    > Hi all,
    >
    > I'm new to perl (that's why I'm here :-).
    ....and you are getting your first opbject lesson:
    use strict;
    use warnings;
    >
    > $tmp_file="users.tmp";
    >
    > open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
    Note $tmp_file
    > rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
    > '$data_file': $!\n");
    There is no $temp_file.

    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139