deleting data in text file

well, i have a homepage whereby users can key in data and that data will automatically be in my text file. i use pipe and the sample of the text file is like this:

aa|bb|cc|dd
ee|ff|ggg|fdg
rr|fdg|tfh|fcbv

from the web, user has to enter for fields, and that data will be inserted in the text file as above. it's working as what i wanted.
the problem is when i want to delete let's say the second row, how could i do it? can i do so? should i replace that row to blank? is there any delete command in perl??
answer with coding is really appreciated.
bye
kudakAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TYoungConnect With a Mentor Commented:
open(FILE, "file.txt");
my(@lines) = <FILE>;
close(FILE);

$LineToDelete = '2';

$Numbs = @lines;

open(FILE, ">file.txt");
     for($i = 0; $i < $Numbs - 1; $i++) {
          print FILE @lines[$i] unless ($i = ($LineToDelete - 1));
     }
close(FILE);



THIS IS UNTESTED... But you get the idea... write everyline over, except the one you want to delete.
0
 
Kim RyanIT ConsultantCommented:
It's a common question and has been covered in the Perl FAQ:
http://www.perl.com/pub/doc/manual/html/pod/perlfaq5.html#How_do_I_change_one_line_in_a_fi
0
 
cadabraCommented:
You will need a synchronization mechanism, to serialize access to the file;

See Win32::Mutex , if you are using activestate perl (on windows platform).
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kudakAuthor Commented:
ok tyoung. let's say i that data is keyed in by the user. is it going to be

open(FILE, "file.txt");
my(@lines) = <FILE>;
close(FILE);

$LineToDelete = '(FORM{name} eq "xx") && (FORM{address} eq "zz"';

$Numbs = @lines;

open(FILE, ">file.txt");
     for($i = 0; $i < $Numbs - 1; $i++) {
          print FILE @lines[$i] unless ($i = ($LineToDelete - 1));
     }
close(FILE);

thanks.


0
 
TYoungCommented:
Okay, then you would want something like this:

open(FILE, "file.txt");
      my(@lines) = <FILE>;
close(FILE);
my($i) = 0;
foreach (@lines) {
      $FILELINES{$_} = $i + 1;
      $i++;
}
my($LTD) = $FORM{'name'} . "|" . $FORM{'address'} . "\n";
$LineToDelete = $FILELINES{$LTD};
$Numbs = @lines;
open(FILE, ">file.txt");
     for($i = 0; $i < $Numbs - 1; $i++) {
          print FILE @lines[$i] unless ($i = ($LineToDelete - 1));
     }
close(FILE);

Again.. this is untested...
0
 
cadabraCommented:
how about using grep() to remove the unwanted lines from @lines ...
0
 
TYoungCommented:
If you use grep, you may delete some line of a person that has the same first name, or same zip code, or something like that.
0
 
ozoCommented:
#You could grep for the same condition you're testing in your for loop
#or
{local $^I=$$; local @ARGV=("file.txt");
while( <> ){ print unless /^\Q$FORM{name}|$FORM{address}|\E/ }
}
0
All Courses

From novice to tech pro — start learning today.