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: Beginner:
Deleting from data file...?? - Syntax help...

 



Tim
Deleted

Feb 16, 2000, 11:39 PM

Post #1 of 8 (1506 views)
Deleting from data file...?? - Syntax help... Can't Post

I'm trying to delete a user from a flat file database. It is working for me but it isn't deleting the line returns. How do I get the code below to delete the "/n"?

$username is obtained from an admin form.

open PWD, "+<$members_file";
flock (PWD, 2);
$_ = do { local $/, <PWD> };
$found = s/^\Q$username\E:.*$//mg;
seek PWD, 0, 0;
truncate PWD, 0;
print PWD;
flock (PWD, 8);
close PWD;

if ($found) { print "$username deleted" }
else { print "$username not found" }


For the sake of learning, what are some other methods or code that can be used to delete from a flat file database. I've seen a lot of different methods used, but I'm unsure what is the "best" one.

Thanks,

Tim


perlkid
stranger

Feb 17, 2000, 1:18 PM

Post #2 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

 
Ok, for the moment I don't know that answer. But eventually I will have to learn and when I need to know I will probably go to

http://www.gossamer-threads.com

and down load dbman from there scripts and take a look at how the do it because the program does nothing but add modify and delete records from a file. So I think you should go tear apart there script and just throw away the parts you don't like. The guy who writes those programs is a pretty sharp
programmer so I would expect that he is using the most efficient methods.

sorry I couldn't be of more use,

perlkid


kencl
User

Feb 17, 2000, 2:07 PM

Post #3 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

You have to rebuild the list in memory.

foreach $entry (@OriginalData) {
($testkey,$forget1,$forget2) = split /, /, $entry;
if ($testkey ne $username) {
push(@NewData, $entry);
}
}


Cure
User

Feb 17, 2000, 2:17 PM

Post #4 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

Hi

#!/usr/bin/perl

open (PWD, "<$members_file") or die "couldnt open $members_file: $!";
flock PWD, 2;
while (<PWD> ) {
next if (/^$username/);
$found .= $_;
}
flock PWD, 8;
close PWD;

open (PWD, ">$members_file") or die "couldnt open $members_file: $!";
flock PWD, 2;
print PWD $found;
flock PWD, 8;
close PWD;


Cure


japhy
Enthusiast

Feb 17, 2000, 3:04 PM

Post #5 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

RACE CONDITION!
RACE CONDITION!
RACE CONDITION!

That's dangerous, Cure. Try something like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local ($^I, @ARGV) = ("", $filename);
while (<> ) {
# why use a regex, you know?

# if you wanted to, it's best to do
# print unless /^\Q$username/o;
# the /o means "$username is not going
# to change, so don't keep rebuilding
# the regex"

print unless index($_, $username) == 0;
}
}
</pre><HR></BLOCKQUOTE>

Randal Schwartz wrote a great column on in-place editing: http://www.stonehenge.com/merlyn/UnixReview/col23.html

Oh, and no, you don't need to rebuild the list in memory. Tim -- I answered this post on CIWAC (comp.infosystems.web.authoring.cgi).

[This message has been edited by japhy (edited 02-17-2000).]


Tim
Deleted

Feb 18, 2000, 2:15 AM

Post #6 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

Hi,

Thanks for the replies everyone!

japhy, for some reason when I go to the newsgroup I only see my message..no replies. I can get new messages but no replies to my post appear...strange!

Anyway, I'm new to programming and am trying to learn as much as possible...I assume the code you wrote is "better" than the code I was trying to use. If I'm correct in that assumption, what makes it more desirable to use?

Thanks,

Tim



japhy
Enthusiast

Feb 18, 2000, 5:42 AM

Post #7 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

Tim, the following code is just fine:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use Fcntl ':flock'; # for LOCK_EX

open PWD, "+<$members_file";
flock PWD, LOCK_EX;
$_ = do { local $/, <PWD> };
$found = s/^\Q$username\E:.*\n//mg;
seek PWD, 0, 0;
truncate PWD, 0;
print PWD;
close PWD;

if ($found) { print "$username deleted" }
else { print "$username not found" }
</pre><HR></BLOCKQUOTE>

I changed the regex (removed the final $) to make it work as you asked. You should use the Fcntl.pm constant LOCK_EX instead of 2 for flock(), and you should not unflock a file manually. Perl does it for you.

This is a perfectly fine method of doing this. You can also do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local ($^I, @ARGV) = ("", $members_file);
/^\Q$username\E:/ or print while <>;
}
</pre><HR></BLOCKQUOTE>

There's magic going on there -- explainable magic, but magic nonetheless -- and it's Perl's version of in-place editing. It's rather graceful.


Tim
Deleted

Feb 18, 2000, 11:19 AM

Post #8 of 8 (1506 views)
Re: Deleting from data file...?? - Syntax help... [In reply to] Can't Post

Hi,

Thanks! That is great. The code you wrote does look like magic! There's hardly anything there...I'll begin my research to figure what's going there.

How do I add the success or error at the end? if ($found) { print "$username deleted" }
else { print "$username not found" }

Thanks,

Tim


 
 


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

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