Gossamer Forum
Home : General : Perl Programming :

Deleting one line...

Quote Reply
Deleting one line...
Sorry for all my questions I ask..

but I am stuck working to delete a line from a flat file db..

I use this code right now but it always deletes the last line or first line and not any other one.

Code:
use Fcntl qw(:DEFAULT :flock);

sysopen(FILE, "$datapath/members/carts/$FORM{'username'}.txt", O_RDWR) or die "Can't open: $!";
flock(FILE, LOCK_EX);

$ItemIndex = "$FORM{'item'}|$FORM{'num'}|$FORM{'price'}|$FORM{'desc'}|$FORM{'quant'}|";

@FileContents = <FILE>;
@DeletedItem = splice(@FileContents, $ItemIndex, 1);

seek(FILE, 0, 0) or die "Can't return to beginning: $!";
truncate(FILE, 0) or die "Can't truncate file to zero length: $!";
print FILE @DeletedItem;

flock(FILE, LOCK_UN);
close(FILE);
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Oct 18, 2001, 3:55 PM
Quote Reply
Re: [nolimit] Deleting one line... In reply to
If the lines begin with a unique value you can use grep

Something like.....

@new_file = grep { !/^LINE_YOU_DONT_WANT/ } @old_file;
Quote Reply
Re: [RedRum] Deleting one line... In reply to
Thanks for the response, but I tried

open(FILE, ">$datapath/members/carts/$FORM{'username'}.txt") or die "Can't open: $!";
@old_file = <FILE>;

@new_file = grep { !/^$FORM{'item'}|$FORM{'num'}|$FORM{'price'}|$FORM{'desc'}|$FORM{'quant'}|/ } @old_file;

print FILE @new_file; # Write the new data into the file.

close(FILE);

And it deletes the whole file.
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Deleting one line... In reply to
Well yeah because you opened the file for writing with > - thats gonna delete it before ya started with my code :)
Quote Reply
Re: [nolimit] Deleting one line... In reply to
This should help:

http://www.perldoc.com/...beginning-of-a-file-
Quote Reply
Re: [RedRum] Deleting one line... In reply to
I tried this but it still deleted the whole line:

Code:
open(OLDFILE, "<$datapath/members/carts/$FORM{'username'}.txt") or die "Can't open: $!";
@old_file = <OLDFILE>;
@new_file = grep { !/^$FORM{'item'}|$FORM{'num'}|$FORM{'price'}|$FORM{'desc'}|$FORM{'quant'}|/ } @old_file;
close(OLDFILE);
open(FILE, ">$datapath/members/carts/$FORM{'username'}.txt") or die "Can't open: $!";

print FILE @new_file; # Write the new data into the file.

close(FILE);
-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central
Quote Reply
Re: [nolimit] Deleting one line... In reply to
No one knows why?

I'm wanting to fix this bad so whoever gives me something that works I'll give ya some free ad space..

-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Oct 21, 2001, 5:58 PM
Quote Reply
Re: [nolimit] Deleting one line... In reply to
open(OLDFILE, "<$datapath/members/carts/$FORM{'username'}.txt") or die "Can't open: $!";
@old_file = <OLDFILE>;
close OLDFILE;

@new_file = grep { !/^$FORM{'item'}\|$FORM{'num'}\|$FORM{'price'}\|$FORM{'desc'}\|$FORM{'quant'}\|/ } @old_file;

open(FILE, ">$datapath/members/carts/$FORM{'username'}.txt") or die "Can't open: $!";
print FILE @new_file;
close(FILE);
Quote Reply
Re: [RedRum] Deleting one line... In reply to
Thanks Paul

Tell me where your banner is and I'll add it.

-------------
Jeremy
http://lc.crashinto.com - Crashinto Learning Central

Last edited by:

nolimit: Oct 22, 2001, 1:53 PM