pls hlp

merlin's picture

They have: 410 posts

Joined: Oct 1999

well, as you already know (probably), i'm learning perl. i tried to write a password/username-checker, a very simple one.
it's almost working, but it doesn't recognize the username from <STDIN> in my file.
here's the code:

chomp ($user = <STDIN> );
chomp ($pw = <STDIN> );
if ($user ne "") {
open (PW, "pw.txt") or die "Can't open file: $!\n";
@file = <PW>;
close(PW);
} else {
print "insert username and pw end with enter";
}

@users = @file;
@passwords = @file;

foreach (@users) {
s/,.*//g;
}
foreach (@passwords) {
s/.*,//g;
}

#the problem part i suppose
if ($user eq @users) {
if ($pw eq @passwords) {
print "login ok";
} else {
print "login incorrect";
}
} else {
print "no username";
}

i know, i could do the whole stuff with %hashes, but i'd like to try it this way. it's just for getting used to the syntax, not for any application.
i know it's not as complicated as orpheus' problem, so i hope you can help me...

[This message has been edited by alibababa (edited 16 June 2000).]

They have: 568 posts

Joined: Nov 1999

Complicated problems suck. Little problems are awesome. I'd just like to mention a book, Learning Perl volume 2.

If you don't already have it I suggest you buy it. That will teach you everything there is to know about Perl.

They have: 850 posts

Joined: Jul 1999

The problem is with

code:

if ($user eq @users) {
if ($pw eq @passwords) {
print "login ok";
} else {
print "login incorrect";
}
} else {
print "no username";
}
[/code]
You are trying to compare the inputted variables to the @arrays containing the different usernames/passwords.  This cannot be done. You would need a foreach(@users) statement to do this.
code:
foreach(@users)
{
   if($user eq $_) {.....}
}
[/code]

But, after this, you are seeing if the inputted password is equal to any other passwords in the array.  This could create some problems, because it is not necesarily $user's password.

Here is what I would do.
code:
#The file has been opened, and @info contains:
@info=("rob,password","jon,password2"); 

print "Enter Username:";
chomp ($user = <STDIN> );
print "Enter Password:";
chomp ($pw = <STDIN> );

if($user && $pw)
{
	foreach(@info)
	{
		if(lc($_) eq lc($user).",".lc($pw))
		{
			print "\nSuccess\n";
			exit;
		}
	}
}
else
{
	print "\nYou did not enter all the information required\n";
}
[/code]

Hope that helps.


------------------
click here to help save lives
http://www.wiredstart.com  : The Technology Start Page 
merlin's picture

They have: 410 posts

Joined: Oct 1999

well, thank you robp.
orpheus: i know, i know, my problems are all soluble with reading books. and i do read books (programming perl, o'reilly/perl cookbook, o'reilly), but sometimes i'm just too impatient to read me through the book to solve those little problem, when i know you outthere know the solution. ok, too impatient with myself too...
so i ask you to be my teachers and help me. at this place, i'd like to thank you all for helping me and my impatience...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.