in reply to Re: Deleting a record from a flat text file
in thread Deleting a record from a flat text file

the code is as follows .... i have a problem to build the the second part with the first, to write back ,...
#!/usr/bin/perl # e_id="10.20"; # # open (DATABASE, "mybase.txt"); while (<DATABASE>) { $row = $_; chop $row; ($id2, $status, $fromemail, $efrom,) = split(/\|/, $row); if ($form{'e_id'} ne $id2) {$new_row .= "$row\n";} } close (DATABASE); #writing the '$new_row' to the textfield:<p> open (DATABASE, ">mybase.txt"); print DATABASE "$new_row"; close (DATABASE);

edited: Sat Jun 14 14:05:07 2003 by jeffa - code tags

Replies are listed 'Best First'.
Re: Re: Re: Deleting a record from a flat text file
by vek (Prior) on Jun 13, 2003 at 18:52 UTC

    You have commented out the first call to open which means the mybase.txt is not being read. Then you clobber the file with your second open.

    Couple of things I'd point out. Always, always, always, check the return value of open, you'll save yourself a lot of grief:

    open(DATABASE, "mybase.txt") || die "open mybase.txt - $!\n";
    Try not to use chop to remove newlines, use chomp instead. chop will remove (and return) the last character in the string regardless of whether it's a newline or not.

    -- vek --
Re: Re: Re: Deleting a record from a flat text file
by Muoyo (Novice) on Jun 13, 2003 at 19:14 UTC
    A few things you might want to consider.

    If you're going to be using this script often you might want to use a command line argument for your pattern matching, and you might want to save a copy of the file you're editing to avoid any unpleasant errors (like deleting the whole file). Just a thought.

    -muoyo