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

Hi! i want to delete a certain line from a text file (small one)! I have a form with checkboxes and the line selected(it's only a name) i want to erase it from the text file, but I don't know how to do it!Thank you for your time This what I've tried but it doesn't work at all!
foreach $key(keys(%FORM)){ 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 ($FORM{$key}==1) { $found =1; next; } print NEW; }} if ($found) { rename ("$file.new", $file) or die "Can't rename new $file: $ +!\n"; } else { print "I didn't find !\n"; }

Replies are listed 'Best First'.
Re: deleting a line from a text file
by rinceWind (Monsignor) on Sep 22, 2003 at 10:10 UTC
    bory, please can you explain in more detail what you are trying to do.

    Firstly, the code is iterating keys(%FORM) and doing a sweep of the file for each one. Note, the close brace for this is the second one on the line following print NEW; hence the rename doesn't happen until all keys have been processed (overwriting $file.new each time).

    Secondly, your condition check does not depend on the text in the input file!

    if ($FORM{$key}==1) { $found =1; next; }
    Finally, you should consider using strict and warnings. This will help you by catching many bugs at source. There are many nodes on PM that describe why strict and warnings are a good thing.

    Hope this helps,

    rinceWind

    --
    I'm Not Just Another Perl Hacker

Re: deleting a line from a text file
by zby (Vicar) on Sep 22, 2003 at 09:59 UTC
    The loop over the %FORM hash keys is really confusing. I think you don't need it. From your description you want to check the lines from the input file against the hash keys, but you never do that. I think you need something like that (not tested):
    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 ($FORM{$_}==1) { $found =1; next; } print NEW; } } close NEW; close OLD; if ($found) { rename ("$file.new", $file) or die "Can't rename new $file: $ +!\n"; } else { print "I didn't find !\n"; }
    What I changed:
    • if ($FORM{$_}==1) this is checking if the line is a key in the %FORM hash and has a value of 1 (you might change it to  if(exists($FORM{$_})) if you need to check just existence).
    • Closing the NEW and OLD filehandles.

    You could look at File::Atomic if you run this concurrently.

Re: deleting a line from a text file
by CountZero (Bishop) on Sep 22, 2003 at 14:31 UTC

    Yes, indeed you have the monks confused!

    Perhaps it helps if you explain what is in your FORM-hash. What are the keys and their values? How does this hash relate to the text file? Has each line of the file a direct one-to-one relationship with the FORM-hash? How do you go from the check-boxes in your form to the FORM-hash? Am I right in assuming this is something related to a web-page/CGI-script?

    So many questions ...

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      "AB174321","RANDOLPH P. QUINCY",4,75 "BF234567","SANDRA J. RAMERIZ",3,65 "NO381209","KATHLEEN T. ADAMS",5,70 "PQ473215","THOMAS A. STEVENS",2,64 "HZ496531","PATRICIA J. CORY",1,70