Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: No <STDIN> yeilds username and password

by eric256 (Parson)
on Feb 10, 2008 at 21:32 UTC ( [id://667295]=note: print w/replies, xml ) Need Help??


in reply to No <STDIN> yeilds username and password

The first thing I did was add strict and warnings. This pointed out several instances where variables weren't declared using my. So I added my in there. A nice little shortcut is that you can actually us it in your chomp (my $fname =<STDIN>); statement. See how I put my in there? Now $fname is declared in that block. I did that for all your input statements.

Then I noticed all of your last blocks, I don't know what you thought they where doing, but as far as i can tell they simple exited the block that they where in (which didn't do anything because last was the only thing in that block. At this point the entire top section of your code worked but now I got errors when you where inserting into the hash. At this point it finally donned on my why you had a loop at the top, I am assuming that you wanted to allow a person to add several users? So moving that assignment up into the loop and re-arranging your exit condition got me that (lines 26-31 in the code below).

Now I started on your actual question as posted here and looked at the lower block. I noticed that lines 50-52 would print for every user that didn't match your criteria, and that didn't seem right, so I moved them out of the loop and added a $found variable to keep track of how many users actually matched the search criteria you provided. Now the search was working good, but searching for a blank line, space, or comma would return every user that was entered (since they would all have a comma and space between the names.) So right after your line 45 I added next unless $_ =~ /\w/; This says to go back to the top of the loop (next) unless the search criteria $_ contained at least one word character (\w which is 0-9, a-z and A-Z).

A few more cosmetic changes, and some verbiage changes and you get the code below

#!/usr/bin/perl # always use strict and warnings use strict; use warnings; #hash to store names my %name_hash; print "Type 'exit' at any time to end this program\n"; #loop to add new users while(1) { print "Please enter your first name\n"; chomp (my $fname =<STDIN>); exit if $fname eq 'exit'; print "Please enter your last name\n"; chomp (my $lname =<STDIN>); exit if $lname eq 'exit'; print "Please enter a password\n"; chomp (my $password =<STDIN>); exit if $password eq 'exit'; my $username = "$lname, $fname"; $name_hash{$username} = $password; print "Please enter 'done' to start seachings or hit enter to a +dd another user.\n"; chomp (my $ending =<STDIN>); exit if $ending eq 'exit'; last if $ending eq 'done'; } print "To search for a user please type in a name and hit enter.\n"; while(<STDIN>) { chomp; exit if $_ eq 'exit'; next unless $_ =~ /\w/; #require at least one letter in the qu +ery my $found = 0; foreach my $key (keys %name_hash) { # guard the regex with \Q so that users can't entere th +eir own regex if ($key =~ m/\Q$_\E/i) { print "The username is '$key' and the password +is '" . $name_hash{$key} . "'\n"; $found++; } } if ($found) { print "I found $found users matching that search. "; } else { print "Sorry no users where found matching '$_'. "; } print "Please try another name to search or type exit to end\n" +; }

I believe this performs closer to your desire. The name input is still kinda of awkward but thats a minor fix. I know that everyone learns different and sometimes it helps to have someone else fix the problem their way, now you can go back and make whichever changes to your original code while having a solutions that works (hopefully as you expected) ;). Welcome to perl and PerlMonks and enjoy!


___________
Eric Hodges

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found