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: Intermediate:
Testing Database file with and only printing if found

 



CuzDesign
Deleted

Jun 6, 2000, 2:26 AM

Post #1 of 2 (565 views)
Testing Database file with and only printing if found Can't Post

Hello,
I have a question here and I cannot seem to figure it out for the life of me. Here is what I am trying to do.

I want to test the contents of a database file it is a username and password file. I know hoe to check if each there is a match but what happens is this.

Say tehre are 5 records in the databse like so.

name:password
name:password

Now I want to go through all the entries and if the proper username and password was entered print one thing if not print another but what is happenning is that it is printing the nomatch over and over and over again. How can I get this to check the database then if finds a match print one thins only once, but say if there are no matches print another only after it has tested the complete database file for a match then print. Here is what I am using now.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (DATA,"<passme.dat") or die "Couldn't open passme.dat $!\n";
while (<DATA> ){
chomp;
@list = split(/:/,$_);
if (($user eq $list[0]) && (crypt($password, $salt) eq $list[1])) {
print "ok\n";
}else{
&error;
}
}
</pre><HR></BLOCKQUOTE>

Any help here would be greatly appreciated.

Thank you,
Ray


monocle
User

Jun 6, 2000, 5:56 AM

Post #2 of 2 (565 views)
Re: Testing Database file with and only printing if found [In reply to] Can't Post

ray,

you need to move the print out of the while loop. I suggest setting a variable before the loop (ie. $found=0) then when you find the match, set it to sometihng else (ie. $found=1). You might also want to use last; when you do find the match. This will cause the loop to terminate and prevent you from stepping through the rest of the list. This can save time if your list is long and you match on line 3. Then after the loop, check if $found is 1 or 0. This will tell you if you found a match and you can act arrordingly.

hope this helps.

Monocle
Hear great techno by Monocle at http://www.mp3.com/monocle

 
 


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

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