Gossamer Forum
Home : General : Perl Programming :

madtech

Quote Reply
madtech
I am trying to stop duplicates within a small flatfile database and writing (as a learning process) but when I tryed to modify the script to incude code to watch for duplicates it stopped functioning. Can you look at it and offer advise?

Here is what I had, and what I did to it...

http://www.mainegoods.com/pub/dupcheck.txt
Quote Reply
Re: [madtech] madtech In reply to
You have @DB = <DATABASE1>; but then foreach $rec (@ODB){

You could speed up the code using a while loop instead rather than pushing all the data into an array first.

You could change:

print DATABASE1 "$input{'myname'}|$input{'myalias'}|$input{'myemail'}|$input{'myicq'}|$input{'myphone'}|$input{'myage'}|$input{'mylocal'}|$input{'myconfirm'}|$input{'mypay'}\n";

to....

print DATABASE1 join('|', values %input) . "\n";

Last edited by:

PaulW: Nov 29, 2001, 3:15 PM
Quote Reply
Re: [PaulW] madtech In reply to
Paul, the order in which the keys are output is unreliable if you don't do some kind of sorting...

Code:
my %def = (
0 => "myname", 1 => "myalias", 2 => "myemail", 3 => "myicq", 4 => "myphone",
5 => "myage", 6 => 'mylocal', 7 => "myconfirm", 8 => "mypay"
);

print DATABASE join ("|", map { $input{$def{$_}} } sort keys %def) . "\n";

--Philip
Links 2.0 moderator

Last edited by:

ThatPerson1024: Nov 29, 2001, 4:55 PM
Quote Reply
Re: [ThatPerson1024] madtech In reply to
Yes I realise that. I was providing the example assuming that there was no specific order.

However you'll find that in your example sort isn't needed Tongue...as hashes sort numerically automatically

%foo = ( 1 => 'foo', 5 => 'bar', 3 => 'bla' );

print join '|', keys %foo;

.....would print 1|3|5

Also your example won't work.

You'd need:

print join ("|", map { $def{$_} } keys %def) . "\n";

...which is kind of pointless as it will work the same as my example

Let's compare the output Laugh

my %def = (
0 => "myname", 1 => "myalias", 2 => "myemail", 3 => "myicq", 4 => "myphone",
5 => "myage", 6 => 'mylocal', 7 => "myconfirm", 8 => "mypay"
);

print join ("|", map { $def{$_} } keys %def);
print join ("|", map { $def{$_} } sort keys %def);
print join ("|", values %def);

........

myname|myalias|myemail|myicq|myphone|myage|mylocal|myconfirm|mypay
myname|myalias|myemail|myicq|myphone|myage|mylocal|myconfirm|mypay
myname|myalias|myemail|myicq|myphone|myage|mylocal|myconfirm|mypay

I win


Last edited by:

PaulW: Nov 29, 2001, 6:17 PM
Quote Reply
Re: [PaulW] madtech In reply to
First I'd like to thank you for the replys..

I did fix that ODB typo, but still didnt work.. For a better idea I put up another .txt file.. Please see..

http://www.mainegoods.com/pub/betteridea.txt

My goal is to simply find a way to check the input name variable with the current names in that database.
Quote Reply
Re: [PaulW] madtech In reply to
Righto. But at least my example forces the output in a predetermined order, whereas your initial example does not. In fact, none of your examples work...

--Philip
Links 2.0 moderator
Quote Reply
Re: [madtech] madtech In reply to
Code:
if ($input{'action'} eq 'add'){
open (DATABASE1,">$database");
@ODB=<DATABASE1>;
foreach $rec (@ODB){
chomp($rec);
($name,$alias,$email,$icq,$phone,$age,$local,$confirm,$pay)=split(/\|/,$rec);
if ($name eq $input{'myname'}){
print "Sorry\n";
}else{
print DATABASE1 "$input{'myname'}|$input{'myalias'}|$input{'myemail'}|$input{'myicq'}|$input{'myphone'}|$input{'myage'}|$input{'mylocal'}|$input{'myconfirm'}|$input{'mypay'}\n";
}
}
close (DATABASE1);
}

You're opening the file for writing, thus wiping out everything that's already in there.

Since you have random length lines you can't use a read/write mode. Read in the array, open a new file for writing, loop thru the array and put the entires you want into the new file. you can then move the new file into place over the old file (you shuld move the old to .bak too).
Quote Reply
Re: [ThatPerson1024] madtech In reply to
>>Righto. But at least my example forces the output in a predetermined order, whereas your initial example does not.<<

Your example would only work if madtech used numbers as keys, as would mine so it makes no difference.

>> In fact, none of your examples work...<<

If you are talking about the 3 examples above then yes they do Crazy.

Last edited by:

PaulW: Nov 30, 2001, 4:22 AM