Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found