Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Code Optimization v5.8.1

by dragonchild (Archbishop)
on May 20, 2004 at 17:33 UTC ( [id://355028]=note: print w/replies, xml ) Need Help??


in reply to Code Optimization v5.8.1

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

Replies are listed 'Best First'.
Re^2: Code Optimization v5.8.1
by Nkuvu (Priest) on May 20, 2004 at 17:48 UTC
    I like this style, but the one thing that would drive me crazy using a program like this is that I wouldn't have any idea of how to exit. While I don't like cumbersome prompts like "Enter an acronym, upper or lower case, or enter something that starts with Q (also upper or lower case) to quit:" perhaps some indication of how to get out would be beneficial?

    I know this is probably a moot point for a script like this, but I spend too much time in programs with really bad UIs to ignore it...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://355028]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found