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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Testing Database file with and only printing if found



Jun 6, 2000, 2:26 AM

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

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.


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> ){
@list = split(/:/,$_);
if (($user eq $list[0]) && (crypt($password, $salt) eq $list[1])) {
print "ok\n";

Any help here would be greatly appreciated.

Thank you,


Jun 6, 2000, 5:56 AM

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


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.

Hear great techno by Monocle at


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

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