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

if($q->param('do') eq "Login") { open(USER, "user.dat"); @user = <USER>; close(USER); if(!$q->param('user')) { &error("You need to enter a username"); } elsif(!$q->param('pass')) { &error("You need to enter a password"); } else { foreach $line (@user) { chomp($line); ($name, $site, $site_id, $des, $email, $pass) = split(/\|/,$ +line); $pass2 = crypt($q->param('pass'), r); if($q->param('user') eq $site_id) { if($pass2 eq $pass) { &start; } else { &error("Password or Username is Incorrect"); } } } } }
Ok Now in this part
if($q->param('user') eq $site_id) { if($pass2 eq $pass) { &start; } else { &error("Password or Username is Incorrect"); } }
How can I say "if($q->param('user') ne $site_id)" it's in a foreach loop, if I do something else it will print it like 20 times.

Replies are listed 'Best First'.
Re: working with foreach, nestd if's inside
by VSarkiss (Monsignor) on Jun 11, 2002 at 23:28 UTC

    Anonymous Monk, you've been posting this same question, or variations of it to Perl Monks Discussion, usually at least three copies of each one. I know because I've been one of the locals helping to clean up behind you. You posted a dupe of this while I was writing this reply. iceraider did the same thing earlier -- are you in fact the same?

    Perl Monks Discussion is for discussions about this site, please don't post general questions there.

    Please wait for your question to show up before posting it again.

    Please read the answers to your previous questions before repeating them.

Re: working with foreach, nestd if's inside
by thunders (Priest) on Jun 11, 2002 at 23:41 UTC
    update: When I posted this answer I didn't realize this was in discussions, I followed a CB link, this should be moved or reaped

    I'm not sure I get your question but If you have problems exiting a loop I think you need to read about flow control, see next and last (and also continue) availible via perldoc -f for more info on the topic.

      foreach $line (@user) { chomp($line); ($name, $site, $site_id, $des, $email, $pass) = split(/\|/,$ +line); $pass2 = crypt($q->param('pass'), r); if($q->param('user') eq $site_id) { if($pass2 eq $pass) { &start; } else { &error("Password Incorrect"); } } else { &error("Username does not exist"); last;} } }
      --------
      else { &error("Username does not exist"); last;}
      I tried last and it prints once, but it alwats says Username does not exist, I can't get around it.