Gossamer Forum
Home : General : Perl Programming :

Prevent Duplicate Enteries

Quote Reply
Prevent Duplicate Enteries
A simple addition of a couple of records to a database file?



$newfile =
"newfile.db"; .


open(TXT,
">>/home/cgi-bin/db/$newfile") #


print TXT
"$in{'ID'}|$in{'DD'}|$in{'date'}|$in{'pname'}\n" ;

close (TXT) ;




The problem is I want to avoid duplicate entries for the fields 'pname' and 'date'.

Any solutions?

Thanks,

Zeshan.



Quote Reply
Re: [zeshan] Prevent Duplicate Enteries In reply to
these will by hash defiinition only have one value. however, if you're repeating this process and resetting the hash values in a loop or something, you will need to make some decisions (like do I want to write out the first time 'pname' is the key or last time it's the key etc...)...also, not clear as to whether you're looking to not repeat values that exist in the file, or just not repeat during script execution. probably would be best if you posted some more code.
Quote Reply
Re: [zeshan] Prevent Duplicate Enteries In reply to
Something like;


Code:
$newfile = "newfile.db"; .
$phrase = $in{'date'} . "|" . $in{'pname'};
$exists = 0;

open(TXT, "/home/cgi-bin/db/$newfile") || die "Unable to open. Reason: $!";
while $line (<TXT>) {
chomp;
if ($line =~ /$phrase/) { $exists = 1; next; }
}
close(TXT);

if (!$exists) {
open(TXT, ">>/home/cgi-bin/db/$newfile") |\ die "Cant open file. Reason: $!";
print TXT "$in{'ID'}|$in{'DD'}|$in{'date'}|$in{'pname'}\n" ;
close (TXT) ;
}

THIS is untested!

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] Prevent Duplicate Enteries In reply to
May I suggest in place of "next" you use "last", Andy? If you already found an existing match, there is no reason to continue looking through the file for a match.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] Prevent Duplicate Enteries In reply to
Very true Laugh

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] Prevent Duplicate Enteries In reply to
It's probably also worth pointing out that:

Code:
while $line (<TXT>) {

..is invalid syntax. You can assign the value to a scalar when using a for/foreach loop but not with a while loop.

You then go on to call chomp() without any parameters which is infact trying to chomp $_ but as you were trying to use $line then that would have no effect.

Thirdly, instead of:

Code:
if ($line =~ /$phrase/) {

...the following may be quicker:

Code:
if (index($line, $phrase) > -1) {

...so including fuzzy thoughts fix it would read:

Code:
last if (index($line, $phrase) > -1);

One final thing I spotted is a typo. You entered |\ instead of ||

Last edited by:

Paul: Mar 18, 2003, 8:21 AM
Quote Reply
Re: [Paul] Prevent Duplicate Enteries In reply to
Yeah...that would help Tongue As I said before, it was untested. I literally just came up with an example to get him on the right tracks :)

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: [Paul] Prevent Duplicate Enteries In reply to
You know! you both Paul and Andy are discussing your programming but I m unable to get what the final code is?

Can you put a final piece of code for me after all the debugging of the code by paul. I m unable to get it. I m really a novice user.

Still looking for your help.

Thanks,

Zeshan.
Quote Reply
Re: [zeshan] Prevent Duplicate Enteries In reply to
All you needed to do was replace the stuff that Paul pointed out Tongue

The final code should look something like;

Code:
$newfile = "newfile.db"; .
$phrase = $in{'date'} . "|" . $in{'pname'};
$exists = 0;

open(TXT, "/home/cgi-bin/db/$newfile") || die "Unable to open. Reason: $!";
while (<TXT>) {
chomp;
if (index($_, $phrase) > -1) { $exists = 1; last; }
}
close(TXT);

if (!$exists) {
open(TXT, ">>/home/cgi-bin/db/$newfile") || die "Cant open file. Reason: $!";
print TXT "$in{'ID'}|$in{'DD'}|$in{'date'}|$in{'pname'}\n" ;
close (TXT) ;
}

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!