Solved

Scalar matching and if/elsif statement problems...

Posted on 2004-04-28
3
364 Views
Last Modified: 2013-12-25
Here's my script. I want the script to be able to take the "email" input value and check and see if it already exists in a file, if not, add it, if it does, alert the user that it already exists.

Code:
--------------------------------------------->>
#!/usr/bin/perl -wT

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

use Fcntl qw(:flock :seek);

   print "Content-type: text/html\n\n";


sub add {
open(USER, ">>users.txt") or &dienice("Sorry\, there has been a problem\nadding you to our User File\.\nClick Back on Your Browser and try again\.: $!");
    flock(USER, LOCK_EX);
    seek(USER,0,2);
    print USER param('email');
print USER "\n";
close(USER);

print <<EndHTML;
<html>
<head>
<script>
function goBack(){
  history.go(-1);
}
</script>
</head>
<body onload="goBack();">
</body>
</html>
EndHTML
}


open(USER, "users.txt") or &dienice("Sorry\, there has been a problem\nadding you to our User File\.\nClick Back on Your Browser and try again\.: $!");
flock(USER, LOCK_SH);
seek(USER,0,0);
@list = <USER>;
close(USER);

$email = param('email');
$email =~ s/%(..)/pack("c",hex($1))/ge;

$num = $#list;
for ($i = 0, $i => $num, $i++){
$comp = $list[$i];

if ($comp == $email){
      print "Sorry, there has been an error, the e-mail address you entered already exists. Please Click Back on Your Browser and try again.<br>";
     last;
}
elsif ($i => $num){
     add();
     last;
}
}
---------------------------------------------<<

Can anyone tell me why the following statement always comes up true, despite the user's e-mail not being in the file?
--------------------------------------------->>
if ($comp == $email){
      print "Sorry, there has been an error, the e-mail address you entered already exists. Please Click Back on Your Browser and try again.<br>";
     
last;
}
---------------------------------------------<<

And if I change it to the "eq" string comparison it always returns false, even when the e-mail address is in the file.

It was also suggested to me to use "List::Compare" with the "get_union" function, only to find that my Hosting company doesn't have the "List::Compare.pm" module installed.

Any ideas?
0
Comment
Question by:ziffgone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 125 total points
ID: 10943254
$comp == $email is always true in your situation, because you are dealing with strings.
== test for _numerical_ equality, and most strings evaluate as 0 when coerced into a numerical context.
What you want is a string equality test, and that's what eq is for:
$comp eq $email.

There is no great need for external modules, but you may improve the performance by not preloading the entire file. Instead, process the file line by line, and quit when you have a match. Let me sketch this idea out for you:

$email = param('email');
$found = 0;
open USER, "users.txt";
while(<USER>) {
   chomp;
   if($email eq $_) {
      print "Already in there!";
      $found = 1;
      last;
   }
}

unless($found) {
    add();
}
0
 
LVL 14

Author Comment

by:ziffgone
ID: 10944676
Would the "unless($found)" statement come outside "close USER;" then? As the add(); function will write to the file when "$email" is not found.
0
 
LVL 14

Author Comment

by:ziffgone
ID: 10944726
Never mind, tested it myself.

BING, BING, BING, BING....

kandura gets the points!

I wanted to use the "while" function anyway, so your solution is a perfect fit.

Thanks a b'zillion kandura!

Regards...
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question