Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[SOLVED] Checking username and password but cant break whileloop!

by jaffinito34 (Acolyte)
on Nov 09, 2012 at 15:52 UTC ( #1003171=perlquestion: print w/ replies, xml ) Need Help??
jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to compare the username entered to the username in a file along with the password. The script can check the file and return whether its a match or not but whenever the username does not match, the while loop prints "...does not match..." for each user in the file rather than just once. Here's my code:

#! /usr/bin/perl # CSC 310 Project # 3 #opening passwd file open (PSWD, '<', 'passwd.txt'); #getting username and password #converting username to lowercase if anything is entered in CAPS print "Please enter your username: "; chomp($userN = <STDIN>); $username = lc($userN); print "Please enter your password: "; #hiding password system ("stty -echo"); chomp($passwd = <STDIN>); #reading passwd.txt and assigning values while ($lines = <PSWD>){ ($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $li +nes; #checking username entered vs that in the passwd file if ($username eq $user){ print "Checking username... MATCH\n"; #checking password entered vs that in the passwd file if ($passwd eq $pswd){ print "Checking password... MATCH\n"; } else{ print "Password does not match!\n"; } } else{ print "\"$username\" does not match any users in our database!\n"; } }

The last else statement is what keeps repeating through the while loop but I only want it to print once if the username does not match, not each time that it checks the username against the file. I tried 'last;' but that would kill the while loop immediately so when the username actually does match, it would print that it doesnt because it didnt match the first one in the file. Any help or reference to help is appreciated!

I know the identations are a little off, theyre perfect in my script though

Comment on [SOLVED] Checking username and password but cant break whileloop!
Download Code
Re: Checking username and password but cant break whileloop!
by tobyink (Abbot) on Nov 09, 2012 at 16:06 UTC

    I think you want the last function.

    my $match = 0; # We haven't got a match yet. while ($lines = <PSWD>){ ($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $lin +es; if ($username eq $user) { print "Checking username... MATCH\n"; if ($passwd eq $pswd){ print "Checking password... MATCH\n"; } else { print "Password does not match!\n"; } $match = 1; # We have found a match last; # So stop looping already! } } unless ($match) { print "\"$username\" does not match any users in our database!\n"; }

    Update: I have also moved that else outside of the loop as an unless block.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Same thing happened, it printed "...does not match..." for each user in the file rather than just once. Where you have it is for when the username is correct, which I don't have any issues with. It's when the username entered is incorrect that is giving me problems.

Re: Checking username and password but cant break whileloop!
by NetWallah (Abbot) on Nov 09, 2012 at 16:11 UTC
    You need to change your logic such that the "does not match" message is printed AFTER the WHILE loop completes.

    This means, you need a flag inside the while loop that determines whether or not a match was found.

    Typically, this would be something like a $match_count, which initializes to zero, and is incremented on each match.
    This counter could also be used to exit the loop early, if a match is found (using the 'last' verb).

    At the end of the loop, if it is still zero, you print the message.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

      Genius

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003171]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-11-28 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (194 votes), past polls