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
Subject Author Views Date
Thread Deleting one line... nolimit 4474 Oct 18, 2001, 3:54 PM
Thread Re: [nolimit] Deleting one line...
Paul 4259 Oct 18, 2001, 4:02 PM
Thread Re: [RedRum] Deleting one line...
nolimit 4288 Oct 18, 2001, 4:17 PM
Post Re: [nolimit] Deleting one line...
Paul 4242 Oct 18, 2001, 7:15 PM
Thread Re: [nolimit] Deleting one line...
Paul 4229 Oct 18, 2001, 7:32 PM
Thread Re: [RedRum] Deleting one line...
nolimit 4191 Oct 20, 2001, 3:39 AM
Post Re: [nolimit] Deleting one line...
nolimit 4194 Oct 21, 2001, 5:56 PM
Thread Re: [nolimit] Deleting one line...
Paul 4172 Oct 22, 2001, 4:22 AM
Post Re: [RedRum] Deleting one line...
nolimit 4176 Oct 22, 2001, 1:52 PM