keshu has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to delete a particular record or line from a flat file by taking the name from form input?
  • Download Code

Replies are listed 'Best First'.
Re: How to delete a particular record or line from a flat file by taking the name from form input?
by arturo (Vicar) on Feb 15, 2001 at 04:33 UTC

    Eek! You really *must* wrap your code in <code> and </code> tags to make it readable! Take a gander at the Site How To. Some of your filehandles got 'eaten' (well, aren't displayed, since the browser interprets them as HTML tags it's never seen before).

    You seem to have the basic structure down for what you need to do (you could use a DBM file and tie to get your file to behave like a hash, then all you'd have to do is search for a matching hash key and delete that -- but that would be major retooling, suitable for v 2.0 =) Here's one way to do it (that turns on the key field being the first one in the line!):

    open OLD, $file or die "Can't open $file:$!\n"; open NEW, ">$file.new" or die "Can't open temp file $file.new: $!\n"; my $found =0; while (<OLD>) { if (/^$username\t/) { $found =1; next; # skips to the next line, so won't print # any matching line to the new file } print NEW; # implicit "$_" } if ($found) { rename ($file, "$file.old") or die "Can't back up old $file: $!\n" +; rename ("$file.new", $file) or die "Can't rename new $file: $!\n"; } else { print "I didn't find $username!\n"; }

    HTH.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor