Gossamer Forum
Home : General : Perl Programming :

deleting the record

Quote Reply
deleting the record
Code:
if (param('deletepuser')) {
$deletepuser = param('deletepuser');
open (GUEST,"$private") ||die $!;
flock(GUEST, LOCK_SH);
@lines=<GUEST>;
flock(GUEST, LOCK_UN);
close(GUEST);

open (GUEST,">$private") || die $!;
flock(GUEST, LOCK_EX);
foreach (@lines) {
chomp;
if($_ ne ""){
($a,$b)=split(/__/,$_);
if($deletepuser ne $b){
print GUEST "$_\n";
}
}
}
flock(GUEST, LOCK_UN);
close(GUEST);


above is to deletete record of email in a text file my text file are as

name__email

email__email

so, any similar mail submitted from form will be deleted however

I can only delete the last entry in the record file if it is same submitted from form

the non-last entry in record file cannot be deleted...

anything wrong of my code..........

thanks
Quote Reply
Re: [golden_water] deleting the record In reply to
I'm not entirely sure what your after but this slight modification to your code will remove all occurrences of the email address from the list:

Code:
my $deletepuser = param('deletepuser');

if ($deletepuser) {
open (GUEST,"$private") ||die $!;
flock(GUEST, LOCK_SH);
my @lines = <GUEST>;
close(GUEST);

my @new_lines = grep { ! /^.*__$deletepuser$/ } @lines;

open (GUEST,">$private") || die $!;
flock(GUEST, LOCK_EX);
print GUEST @new_lines;
close(GUEST);
}

You might have to modify it a bit to add the new email address to the list though. Again, I'm not 100% clear on what you're after.

Regards,
Charlie
Quote Reply
Re: [golden_water] deleting the record In reply to
Hello golden_water , You are close and chaz is close.

You may have to consider case , ie BOB#bob.com != bob#bob.com

Code:
#!/perl/bin/perl

# Tested command line with text file on Windows XP
# This is perl, v5.6.1 built for MSWin32-x86-multi-thread
# (with 1 registered patch, see perl -V for more detail)
# Copyright 1987-2001, Larry Wall
# Binary build 635 provided by ActiveState Corp.
http://www.ActiveState.com
# Built 15:34:21 Feb 4 2003

use strict;

my $private ="name_email.txt"; # Your line ommitted as I am on command line and not CGI imput.
# if (param('deletepuser')) {




my $deletepuser = lc("joe\@joe.com");
#my $deletepuser = lc("bob\@bob.com");
#my $deletepuser = lc("mary\@mary.com");
#my $deletepuser = lc("harry\@harry.com");




# Assign strict vars
my ($a,$b,$a2,$b2,$x,$y,$LINES,@LINES);
my ($LOCK_SH, $LOCK_UN, $LOCK_EX);




$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_UN = 8;




# READ CURRENT FILE

open (GUEST,"$private") || die "Unable to open for input file $private: reason $! ";
flock(GUEST, $LOCK_SH);




$x=0;

while (<GUEST>) {
chomp; # remove new line character
($a,$b)=split(/\_\_/,$_); # split the name email pairs
$a2 = lc($a); # LOWER CASE for compare
$b2 = lc($b); # LOWER CASE for compare





if(($deletepuser ne $a2) and ($deletepuser ne $b2)){
$LINES[$x] = "$a\_\_$b"; # use the origional case text
$x++;
} # end of if(($deletepuser ne ... loop




} # end of while (<GUEST>) loop

flock(GUEST, $LOCK_UN);
close(GUEST);




# Write new file without duplicate user from $deletepuser


open (NEWGUEST,">$private") || die "Unable to open for output file $private: reason $! ";
flock(NEWGUEST, $LOCK_EX);

for ($y=0; $y<$x; $y++) {
print NEWGUEST "$LINES[$y]\n";
}

flock(NEWGUEST, $LOCK_UN);
close(NEWGUEST);




# } end of YOUR if (param('deletepuser')) ... loop here ?

# ======================================================

# Name_email.txt contains without first pound character:

#bob__bob@bob.com
#mary__mary@mary.com
#harry__harry@harry.com
#joe@joe.com__JOE@JOE.com
#joe@joe.com__JOE@Joe.com
#joe@joe.com__joe@JoE.coM
#bob__bob@bob.com
#mary__mary@mary.com
#harry__harry@harry.com

Thanks cornball

Last edited by:

cornball: Sep 28, 2003, 8:16 PM
Quote Reply
Re: [cornball] deleting the record In reply to
Thanks for the reply

actually, chaz and mine are same

the code I provide can do all update except the record is email(that is why puzzle me)

I caompare with a index no, names,...all ok for updating

is the email address submitted through form "altered" so in the final stage failed to comapare successfully with record

lower case is the issue??

IE browser issue???

very very puzzle...
Quote Reply
Re: [golden_water] deleting the record In reply to
You may need to add some error checking, to see if its working ok...

Code:
my $deletepuser = param('deletepuser');

if ($deletepuser) {
open (GUEST,"$private") ||die $!;
flock(GUEST, LOCK_SH);
my @lines = <GUEST>;
close(GUEST);

foreach (@lines) {
if ($_ =~ /^.*__$deletepuser$/) { print "Match...\n<BR>"; }
}


my @new_lines = grep { ! /^.*__$deletepuser$/ } @lines;

open (GUEST,">$private") || die $!;
flock(GUEST, LOCK_EX);
print GUEST @new_lines;
close(GUEST);
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] deleting the record In reply to
Thank you

I found the mistake through this code and it is global of $deletepuser

I fail to declare in the very beginning of the code so within the sub routine it cannot detect the required $deletepuser