Forum Moderators: coopster & phranque

Message Too Old, No Replies

limiting the number of lines written to a file

with PERL

         

lindajames

9:42 am on May 9, 2003 (gmt 0)

10+ Year Member



Hi, I have the following code, that adds the value of $email and $password into a text file, but before it adds it, it checks whether the text file contains 4 entries if it does the code displays an error message otherwise it adds the entry.

I need help to modify this code, so that it will also check for duplicates, for example if someone enters an email that already exists in the file.

open(FILE, "emails.dat");
@emails = <FILE>;
close(FILE);
if($#emails >= 4) {
&error;
} else {
open(FILE, ">>emails.dat");
print FILE "$email,$password\n";
close(FILE);
print "Location: $url\n\n";
}
}

I would really appreciate anyones help

Cheers
Linda

PsychoTekk

11:59 am on May 9, 2003 (gmt 0)

10+ Year Member



hi lindajames, maybe this snippet of code can help you out, it's an excerpt of a
script i use so you have to modify it to make it fit you needs

{my $logname; my $string; my $found=0;
open(db, "$logname");
my @Lines = <db>;
@Lines = sort(@Lines);
close(db);
foreach(@Lines,)
{if(index($_,"$string") >= 0)
{$found=-1;}}
if ($found!=-1)
{...add the entry...}
else
{...error message...}

lindajames

1:05 pm on May 9, 2003 (gmt 0)

10+ Year Member



i dont quiet understand your code, and dont understand how i can check if the email entry im making already exists.

any help would be very much appreciated

cheers
linda

Storyteller

8:44 pm on May 10, 2003 (gmt 0)

10+ Year Member



Linda, this will do it:

open(FILE, "emails.dat");
@emails = <FILE>;
close(FILE);
@email_checker{@emails} = ();
if($#emails >= 4 ¦¦ exists $email_checker{$email}) {
&error;
} else {
open(FILE, ">>emails.dat");
print FILE "$email,$password\n";
close(FILE);
print "Location: $url\n\n";
}
}

If don't understand the trick, look up on Perl's "hash slices".

lindajames

9:01 pm on May 10, 2003 (gmt 0)

10+ Year Member



I tried that and im getting the following error message:

Unrecognized character at test.cgi line 246.

if($#emails >= $max ¦¦ exists $email_checker{$email}) {

im assuming the error is coz of the ¦¦? not sure

any comments or suggestions would be much appreciated.

Cheers
linda

Storyteller

1:46 pm on May 11, 2003 (gmt 0)

10+ Year Member



Yes, pipe characters get changed somehow when you post ;( ¦¦ are indeed vertical pipes. Also remove the last curly bracket.