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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
TYoungCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.