http://qs321.pair.com?node_id=355000

bluethundr has asked for the wisdom of the Perl Monks concerning the following question:

Hey Folks! I'm still new here, and new to Perl in general.

Still trying to craft, btw, an online presence here that doesn't annoy too many of us! :) I was thinking of "Perl-21 Lemay p.243 ex 4" (honestly!) but that seemed a little less...catchy? I suppose would be the word. At any rate, if you'd like to holler at me that this would have been a better title, by all means! Fire away! Formatting tips also welcome!

So here's the problem from -duh- "Sams Teach Yourself Perl In 21 Days by Laura Lemay". It's example 4 on page 243.

The problem states:

-------------------------------------------------------

'Write a problem to expand acronyms in its input (for example) to replace the letters "HTML" with "HTML (Hypertext Markup Language)". Use the following acronyms and meanings to replace:

HTML (Hypertext Markup Language)

ICBM (InterContinental Ballistic Missile)

EEPROM (Electrically-erasable programmable read-only memory)

SCUBA (Self-contained underwater breathing aparatus)

FAQ (Frequently Asked Question)'

---------------------------------------------------


I didn't phrase the output in quite the same way, but my answer was close enough in gross concept, IMB. So my question is, no, not for you to write the proggie for me! But rather, I came up with a solution that is a little less than...how shall we say? elegant! I realize that Mike Gancarz according to his "Unix Philosophy" (also new to that!) would disagree with this question, but I would love to know how a vet perl user would approach the problem.

btw, this *works* on my Linux laptop. But unfortunatly I'm visiting with relatives and all they have is dialup! And they don't want me installing all this "hacker stuff" (????!!!!!...and not that they would even likely know it if'n I did!!!) onto their pc. Eh, I'm the guest. Guess I'll just "reinvent the wheel"! Also counter to UNIX ideology, I know. I'm not asking for that kind of help, btw. I plan on visiting an installfest soon. But please try to bear with me! :)

So what I'm trying to say (in my ugly and verbose sort of way...much like my code!) is that this may work, or I may typo in the translation. If so, I accept all criticisms.

Here goes!

---------------------------------------------

#!/usr/bin/perl -w $in = (); # usr input $exit = 'n'; # done yet? $final = (); # final output %acro = (); # hash for acronyms %acro = ( 'HTML' => "Hypertext Markup Language", 'ICBM' => "Intercontinental Ballistic Missile", 'EEPROM' => "Electronically-erasable programmable read only memory", 'SCUBA' => "Self Contained Underwater Breathing Aparatus", 'FAQ' => "Frequently Asked Questions", 'LCARS' => "Library Computer And Retrieval System", # my own goofy ass + addition. Others welcome! 'NASA' => "National Aeronautical and Space Administration" # ditto ); while () { print "\nPlease enter an acronym": "; chomp($in = <STDIN>); print "\n"; foreach $ac (keys %acro) { if ($in =~ /$ac/ig) { $final = "\n$ac is the acronym for $acro{ac} \n\n"; last; } else { $final = "\nSorry. But that is not an acronym that I recognize!\n" +; } print $final; $final = (); print "\nTry again? : "; chomp ($exit =~ /[y|yes]/i { next; print "\n"; last; } print "\n"; last; } <br><br>
------------------------------------------------------

So there ya have it! Ugly crappy code from a total noob! :) But what the hey, it works!

THANKS!