Perl-Sensitive Sunglasses | |
PerlMonks |
Re: No <STDIN> yeilds username and passwordby eric256 (Parson) |
on Feb 10, 2008 at 21:32 UTC ( [id://667295]=note: print w/replies, xml ) | Need Help?? |
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
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
In Section
Seekers of Perl Wisdom
|
|