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


In reply to [SOLVED] Checking username and password but cant break whileloop! by jaffinito34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.