CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
There is a piece of program, but it can't work .help!

 



tack
Novice

Dec 6, 2002, 12:52 AM

Post #1 of 6 (1186 views)
There is a piece of program, but it can't work .help! Can't Post

This the the piece of program:

open(lib,"<xbblib.txt");
%g=<lib>;
if(exists($g{$FORM{'username'}}))
{
print " Welcome $FORM{'username'} back";
}
close(lib);


But it can't judge if $FORM{'username'} exists in %g.What 's wrong with it ? Do you know it ? Help please. Thanks .


Paul
Enthusiast

Dec 6, 2002, 1:12 AM

Post #2 of 6 (1182 views)
Re: [tack] There is a piece of program, but it can't work .help! [In reply to] Can't Post

a) You do no error checking so for all you know the file may not even be opening.
b) Reading into a hash is a little strange and probably not a good way.


Code
my $found = 0; 

open LIB, "xbblib.txt" or die "Can't open xbblib: $!";
while (<LIB>) {
chomp;
if (/^\Q$FORM{username}\E$/) {
$found++;
last;
}
}
close LIB;


Then you check whether $found is 0 or 1 to see if you have a match.


(This post was edited by Paul on Dec 6, 2002, 1:13 AM)


davorg
Thaumaturge / Moderator

Dec 7, 2002, 1:15 AM

Post #3 of 6 (1177 views)
Re: [tack] There is a piece of program, but it can't work .help! [In reply to] Can't Post

What is the format of your file? I assume that you have each user on a separate line. In that case your line

Code
%g=<lib>;

really isn't doing that you think it is. Let's assume we have a file that contains this data:

Code
andrew 
bernard
charles
dave

Then once you've read it in with your code, you'll end up with a has that looks like this:

Code
("andrew\n" => "bernard\n", 
"charles\n" => "dave\n")

There are two major problems with this hash. Firstly half of your keys have actually become values and seconds all of the keys still have a newline character on the end - so they won't match the strings you're getting anyway.

You probably want to do something like this:

Code
open(LIB, "<xbblib.txt") or die $!; 
my @g=<LIB>;
chomp(@g); # remove newlines
my %g;
@g{@g} = (1) x @g;

if($g{$FORM{username}}) {
print " Welcome $FORM{username} back";
}
close(LIB);

Notice that as well as correcting your logic, I have also a) Made the name of your filehandle upper case (this isn't enforced in Perl but is a good habit to get into) and b) checked for errors in opening the file.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Dec 7, 2002, 2:29 AM

Post #4 of 6 (1175 views)
Re: [davorg] There is a piece of program, but it can't work .help! [In reply to] Can't Post


Quote
I have also a) Made the name of your filehandle upper case (this isn't enforced in Perl but is a good habit to get into) and b) checked for errors in opening the file.


Which I already provided :)

And my code doesn't slurp memory.


(This post was edited by Paul on Dec 7, 2002, 2:30 AM)


tack
Novice

Dec 7, 2002, 7:27 AM

Post #5 of 6 (1171 views)
Re: [davorg] There is a piece of program, but it can't work .help! [In reply to] Can't Post

Davorg, thank you very much. You're quite right. You know me very well. I will follow your suggestion and try your code later.


tack
Novice

Dec 7, 2002, 8:13 AM

Post #6 of 6 (1170 views)
Re: [davorg] There is a piece of program, but it can't work .help! [In reply to] Can't Post

my data is like this:

Code
dave 
234324
andy
345654
matt
234245

The odd number lines are usernames, and the even number lines are passwords. But your code:

Code
("andrew\n" => "bernard\n",   
"charles\n" => "dave\n")

maked me found that i had ignored the "\n" in the hash. I have correct my code now . And it works well now . It becomes:

Code
open(lib,"<xbblib.txt");  
%g=<lib>;
if(exists($g{"$FORM{'username'}\n"}))
{
print " Welcome $FORM{'username'} back";
}
close(lib);


and i will add some codes to perfect it to make it can judge the inputting password.

Thank you all very much.


(This post was edited by tack on Dec 7, 2002, 8:16 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives