I rewrote this fairly quickly, putting comments inline. It works, but the thing to consider is that a lot of the changes were stylistic changes. Other monks may have cleaner/better solutions.
#!/usr/bin/perl -w use strict; my $in = (); # usr input my $exit = 'n'; # done yet? my %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", 'NASA' => "National Aeronautical and Space Administration" ); # I personally don't like empty conditions on # while loops. This changed the logic a bit, so # I changed the exit prompt as well. while ($exit !~ /^y/i) { print "\nPlease enter an acronym: "; chomp($in = <STDIN>); print "\n"; # I'm lazy, and don't want to worry about entering # uppercase acronyms. The uc() changes input to # uppercase automagically. Also, this approach # doesn't loop over every key every time. That's # what a hash is best for -- so you don't have to loop # over everything. if (exists $acro{uc($in)}) { # No need to assign to a temp variable, btw. print "$in is the acronym for ", $acro{uc($in)}, " \n\n"; } else { # I also echo input so if the user mistyped the acronym they # may see the error. print "Sorry. But $in is not an acronym that I recognize!\n"; } print "\nFinished? "; # Your original script didn't have any way to input $exit, so # it went into an infinite loop (I am guessing that was a # transcription error) chomp($exit = <STDIN>); }

Ask if any of this doesn't make sense.


In reply to Re: Code Optimization v5.8.1 by Nkuvu
in thread Code Optimization v5.8.1 by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.