CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Ok, I'm stumped again. This time deals withDelete

 



crashinto
Novice

Oct 7, 2001, 4:26 PM

Post #1 of 17 (3143 views)
Ok, I'm stumped again. This time deals withDelete Can't Post

I need to delete one line from a flat file db. But each time I try something different it always deletes the whole file. I have searched this forum and another forum for a similar problem and answer and haven't seen one. Anyway I use this code for delete..:


open(DELETE, ">$datapath/members/carts/$FORM{'username'}.txt");

$line = ($citem, $cnum, $cprice, $cdesc, $cquant) = split /\|/;


if ($FORM{'item'} eq $citem) { chop($line); }


print qq| Your item has been deleted.|;


close(DELETE);




wickedxter
User

Oct 8, 2001, 3:00 PM

Post #2 of 17 (3131 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

open FILE, "</dir/to/file.txt";
while($line = <FILE>){
($citem, $cnum, $cprice, $cdesc, $cquant) =split(/\|/,$line);
if($FORM{'item'} ne $citem){
print FILE "$line";
}
close FILE;
}

this should work.. u might have to full around with it a bit to get it.. If not i got more ideas..



crashinto
Novice

Oct 8, 2001, 3:31 PM

Post #3 of 17 (3130 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

Nope, still deleted the whole file.



wickedxter
User

Oct 9, 2001, 3:46 PM

Post #4 of 17 (3124 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

crap.. i for got somthing and i just cought it..

where the open FILE "+</dir/to/file.txt";
there needs to be a plus in front of the <...

sorry..



crashinto
Novice

Oct 9, 2001, 4:14 PM

Post #5 of 17 (3123 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

I got that when you first said it, but it still deleted the whole file.



yapp
User

Oct 10, 2001, 1:27 AM

Post #6 of 17 (3120 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

He're my quess.

You properly need to check my code for some errors, since I typed it in the webbrowser directly. Can can combine my code with other samples above (in threaded view), where new files are created, to store the new data, and then overwrite the original file.


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

sysopen(FILE, $Yourfile, O_RDWR) or die "Can't open: $!";
flock(FILE, LOCK_EX);

# Get the original data, and change it.
my @FileContents = <FILE>;
my $DeletedItem = splice(@FileContents, $ItemIndex, 1);

# write the new data into the file
seek(FILE, 0, 0) or die "Can't return to beginning: $!";
trucate(FILE, 0) or die "Can't truncate file to zero length: $!";
print FILE @FileContents; # Write the new data into the file.

# unlock so other instances can use the file after out edits.
flock(FILE, LOCK_UN);
close(FILE);

As you can see, the file is never closed, until we're done with it. Meanwhile it's locked, so other programs running at the same moment can't edit the same file at the same time, which results in corrupted data/files.

Hope it helps.




crashinto
Novice

Oct 11, 2001, 2:55 PM

Post #7 of 17 (3109 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

Thanks but that still did the same thing.



yapp
User

Oct 11, 2001, 11:14 PM

Post #8 of 17 (3107 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

What do you mean with that?

P.S. check the perldoc Fcntl, or the perldoc -f sysopen ty manpage for more constants passed through sysopen(). IE: sysopen(.. ..., O_RDWR|O_CREAT), or sysopen(....., O_WRONLY|O_CREAT). The | (bitwise or), combines the bit patterns of the constants (a sort of 'add once' operator).



crashinto
Novice

Oct 12, 2001, 3:26 AM

Post #9 of 17 (3106 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

I meant it still deletes the whole file



yapp
User

Oct 12, 2001, 4:26 AM

Post #10 of 17 (3105 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

Well, I tried my code under UNIX at school here.

There is one typoo in it. I didn't type the 'n' in truncate.

Then It worked.

Did your program still deal with the old (already edited) file? That might explain something, or your program just stopped with the trucate line (where the error is)

Did you run the program directly at the webserver? that's properly the reason why you didn't find out. To get the errors to the browser, type

Code
use CGI::Carp qw(fatalsToBrowser);

at the beginning of the script.

P.S. to test it, let the script print a HTTP header, and some values when the code is running. (like "the deleted item is $DeletedItem") That will make debugging easier Eventually, you print the @FileContents to STDOUT so you can see the result.



crashinto
Novice

Oct 12, 2001, 3:25 PM

Post #11 of 17 (3101 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

It gave me this error...

Illegal division by zero at /home/crashint/public_html/cgi-bin/viewcart.cgi line 67.


The lines around that are

use Fcntl qw(:DEFAULT :flock);

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



yapp
User

Oct 13, 2001, 2:36 AM

Post #12 of 17 (3094 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

I'm not surprised.

Dividing $datapath by members by carts by $FORM{'username'}.txt causes that.

YOU NEED TO PUT QUOTES ARROUND THE STRING!!!! HELLO?
IN MY EXAMPLE IT WAS'T NEEDED, SINCE THERE IS SIMPLY ONE VARAIBLE USED.

String usage:

Code
$text1 = $text2;  
$text2 = "Hello $Username how are you?";
$text3 = qq[Hallo $Username how are you?]; # or any other quote with qq is used


P.S. if you use -w at the #!/usr/bin/perl line, like (FOR EXAMPLE) #!/usr/bin/perl -w there errors migh be caughed.

If you also use the code use strict; in your script, all the variables you use need to be declared with the my statement first. That will eliminate more problems. Example: my $X = 4;, or my($X, $Y, @Array) = (3,5,32,76,"TEST",'tt');



crashinto
Novice

Oct 13, 2001, 2:53 AM

Post #13 of 17 (3093 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

 



(This post was edited by crashinto on Oct 13, 2001, 2:37 AM)


crashinto
Novice

Oct 13, 2001, 3:36 AM

Post #14 of 17 (3092 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

Ok it still doesn't do what I need it to, but it now deletes one line but it always deletes the first line, how I do I assign which line to delete?

Would an if statement work?


yapp
User

Oct 14, 2001, 4:35 AM

Post #15 of 17 (3088 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

Set $ItemIndex to the line number. Mad
Didn't u use strict and -w? that properly should have trapped the error.



crashinto
Novice

Oct 14, 2001, 4:57 PM

Post #16 of 17 (3085 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

I have defined ItemIndex to the line that needs to be deleted. But it still deletes the first line.



yapp
User

Oct 14, 2001, 11:20 PM

Post #17 of 17 (3081 views)
Re: Ok, I'm stumped again. This time deals withDelete [In reply to] Can't Post

I don't get it anymore.

Did you use -w at the #! command line?

Did you type use strict;[/n] (requires all variables declared with my(), and makes sure that you're not making any typoos.

Otherwise, please contact me by <A HREF=mailto:webmaster@cool-programming.f2s.com>webmaster@cool-programming.f2s.com</A>. I'd properly need to see your code.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives