Here's how I would have written it. (This code has been lightly tested.)
use strict; use warnings; $|++; 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", ); INPUT: { print "Please enter an acronym: "; chomp( $_ = <STDIN> ); $_ = uc or redo INPUT; last INPUT if /^Q/; unless (exists $acro{$_}) { print "I don't know what '$_' means.\n"; redo INPUT; } print "$_ ($acro{$_})\n"; redo INPUT; }

Some explanations:

  1. Always begin programs with the top two lines. It's easier to have the compiler do the bookkeeping for you.
  2. The $|++ is to disable output buffering. (q.v. Suffering from Buffering for more info.)
  3. Then, I set up the lookup table for the acronyms.
  4. Now, here's the goofy thing. You'll notice I'm using a named block and using last and redo. (I can't use next cause I'm not in a loop, but redo does something similar.) Sometimes, a certain construct makes a given algorithm easier to express for a given person. I like using named blocks and redo to handle keyboard input. Others like while-loops, goto, and all sorts of other ideas. I like named blocks.
  5. Now, I assign to $_, which has the fancy name of "local topic". This is the default variable for many functions, including regexes.
  6. I want to compare case-insensitively, so I uc the string. (uc defaults to using $_.) Now, if there is nothing, uc will return the empty string. This allows me to use logic short-circuits to redo the loop if nothing was entered. (Try it!)
  7. I allow for a quit scenario by stopping if anything that begins with 'Q' or 'q' is entered. (You might want to change this to allow acronyms that begin with 'Q'.)
  8. I use the exists function to have the hash do the lookup for me (instead of me coding a for loop). This also has the benefit of keeping my hash clean. If it doesn't exist, print a message and redo the block.
  9. If it does exist, print the acronym and definition, then redo the block.

If you have any questions, please don't hesitate to ask!

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested


In reply to Re: Code Optimization v5.8.1 by dragonchild
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.