FemmeGretha has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am writing a simple script for user login. When there is username entered that is not in database, how do I get perl to print just one time

print  “<p>You are not registered.</p>”;

instead of after each checked database line. I had it placed in “else” statement and it would print as many “You are not registered” lines as there are entries in database. Here is the part that I am talking about:
open(TEMP,">$tmpfile") || die "Can't create $tmpfile.\n"; foreach $i (@indata) { chomp($i); ($username,$password) = split(/\|/,$i); if ($username eq $in{'oldname'} && $password eq $in{'oldpw'} && $in{ +'delete'} ne "yes") { print TEMP "$in{'oldname'}|$in{'newpw1'}\n"; print "<p>Your password has been changed.</p>"; print "<p>Thank you!</p>\n"; } elsif ($username eq $in{'oldname'} && $password eq $in{'oldpw'} && +$in{'delete'} eq "yes") { print "<p>Your password has been deleted.</p>\n"; print "<p>Thank you!</p>\n"; } else { print TEMP "$i\n"; } } close(TEMP);
Thank you in advance!

Edit, BazB: added code tags

To clarify:
{'oldname'} , {'newpw1'} and {'oldpw'} comes from input form as user is logging in to change the password.

Replies are listed 'Best First'.
Re: How to verify array fields and print ONE message at the end
by mcogan1966 (Monk) on Oct 28, 2003 at 19:58 UTC
    My recommendation would be to do that before you check anything else. Make it your first 'if' structure. Something like:
    $found = 0; foreach $i (@indata) { chomp($i); ($username,$password) = split(/\|/,$i); if ($in{'oldname'} eq $username) { $found = 1; <i>do your other checks for data</i> . . . } } if (!$found) { print "You are not registered!"; }
    This way, the only time you'll fall into your other data checks is when the user is verified as being in your database. If you get to the end of the list, and still can't find the user, then they must not be registered.
Re: How to verify array fields and print ONE message at the end
by QM (Parson) on Oct 28, 2003 at 20:10 UTC
    The code structure you have is not designed for one print at the end. It's designed to step through a list of username/password pairs.

    Tell us how you plan to use the code you've shown with the feature you're seeking, and we might give a better answer.

    In the meantime, I'm guessing you want something like this:

    open(TEMP,">$tmpfile") || die "Can't create $tmpfile.\n"; $found_user = 0; # flag foreach $i (@indata) { chomp($i); ($username,$password) = split(/\|/,$i); if ($username eq $in{'oldname'}) { $found = 1; last; } } if (not $found) { print TEMP "Username $username is not registered\n"; } close(TEMP);
    Of course, I'm wondering where $in{'oldname'} and @indata come from, or even if they factor into the code your seeking.

    OTOH, if you're looking to incorprate this behavior in your given code example, change the else to something like this (or perhaps insert this in front of it):

    elsif ($username eq $in{'oldname'}) { $found = 1; last; }
    (and keep the if (not $found) block).

    You should probably add a last; to each of the if/elsif blocks, unless you're planning on doing something with every username in @indata.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Hi, Thank you for your advice. I tried both suggestions, and I would wipe out my password database every time. If you have time and interest to look over the complete code, I will post it/send it to you. Please let me know. I would be thankful for any help in saving my sanity. FG