Keep It Simple, Stupid | |
PerlMonks |
Re: Perl program to look into the phone directory and in case of a match, print the name along with the numberby Marshall (Canon) |
on Feb 12, 2017 at 19:21 UTC ( [id://1181858]=note: print w/replies, xml ) | Need Help?? |
Your code appears to do one heck of a lot of unnecessary work!
My brain started hurting trying to figure it out. Rather than
making suggestions to your code, it was easier for me to just
re-code it in the hopes that this will be instructive to you. I guess it is nice that the number of phone book entries appears first. However, I consider it "noise" and throw it away. There is no need to use it and this would not be known in any "real" phone book application. A valid line consists of a name, some space and optionally a phone number. If we have the optional phone number, then this line is a new phone book entry. If it is just a name, then just look up the number in the phone_num hash and print the result. There is no need to "keep track of the sections". Now of course in a "real" application, this would be more complicated because for example you would have to allow for there to be more than one "john" each with a different phone number. I hope this gets your thinking moving in more of a "Perl direction". Have Fun. As a comment, I encourage you to space the code out, using appropriate comments and sometimes just a blank line to separate "thought units". White space consumes no MIPs! But that and comments can be some of the most important "code" that you write!
In Section
Seekers of Perl Wisdom
|
|