Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Simple if Statement Not Acting Right?

by o2bwise (Scribe)
on Aug 18, 2005 at 00:53 UTC ( [id://484649]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

This one bigtime perplexes me. This is for an assignment. For now, I am applying a horrible username password utility. All I am doing is inputing username and password and checking it against the values in a flat file, which I am loading into a hash.

I am using a simple if conditional and it seems it should be passing, but it isn't. Here is a snippet of the code:

print TEST "\n\nJust before if statement:\n"; print TEST "Password entered:\t\t$password\n"; print TEST "Username entered:\t\t$userName\n"; print TEST "Hash value for key $userName:\t$usernameInfo{$userName}\n" +; if ( $password eq $usernameInfo{$userName} )
And here is my TEST output:
Just before if statement: Password entered: phaEdrus3 Username entered: Tony Barbieri Hash value for key Tony Barbieri: phaEdrus3
I am looking RIGHT AT the frigging strings that I am comparing. They are identical. And yet...the conditional does not return a true. It goes to the else section.

Does anyone know what the heck is going on??? Maybe I have to chomp something?

Thanks In Advance,

Tony

Replies are listed 'Best First'.
Re: Simple if Statement Not Acting Right?
by Roger (Parson) on Aug 18, 2005 at 01:01 UTC
    Your print is not precise enough to catch the problem. It doesn't show you if any variable value you printed has empty space in it or not.

    Use print ".... [$userName]\n" and print "... [$usernameInfo{$userName}]\n" instead to be more precise.

    I suspect that your hash value has empty spaces at the end.

      That's it!!! Thanks, man!

      I just learned something. Nice simple test.
Re: Simple if Statement Not Acting Right?
by anthski (Scribe) on Aug 18, 2005 at 01:10 UTC
    It works fine for me when hardcoding the values into a script.

    #!/usr/bin/perl -w use strict; my %usernameInfo; my $password = "phaEdrus3"; my $username = "Tony Barbieri"; $usernameInfo{$username} = $password; if ( $password eq $usernameInfo{$username} ) { print "match\n"; } else { print "no match\n"; }

    So I would suggest that you will need to chomp the values that are either being inputted or being read from your flat file so as to remove the carriage returns (\n's).

    cheers,
    Anth

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found