Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Code Optimization v5.8.1

by simonm (Vicar)
on May 20, 2004 at 18:23 UTC ( [id://355041]=note: print w/replies, xml ) Need Help??


in reply to Code Optimization v5.8.1

Write a problem to expand acronyms in its input...

I think most people would read this as suggesting that it should accept arbitrary text with the acronyms embedded, and modify them in place, not just do a straight lookup.

Under this interpretation, CombatSquirrel's solution is the only correct one in the thread.

Here's a related solution using the -p flag:

#!perl -p s/($regex)/$1 ($acronyms{$1})/g; BEGIN { %acronyms = ( 'HTML' => "Hypertext Markup Language", 'ICBM' => "Intercontinental Ballistic Missile", 'EEPROM' => "Electronically-erasable programmable read only memor +y", 'SCUBA' => "Self Contained Underwater Breathing Aparatus", 'FAQ' => "Frequently Asked Questions", 'LCARS' => "Library Computer And Retrieval System", 'NASA' => "National Aeronautical and Space Administration" ); $regex = join '|', map quotemeta, sort { length($b) <=> length($a) } keys %acronyms; }

Replies are listed 'Best First'.
Re: Re: Code Optimization v5.8.1
by qq (Hermit) on May 20, 2004 at 21:23 UTC

    A quick note to bluethundr to explain whats going on here.

    Perhaps the biggest difference between your program and CombatSquirrel's and simonm's is that they do not prompt for input, but instead read it from stdin. A program that prompts for input can only be used in one way. A program that reads text from stdin, does some work (expands acronyms) and writes out to stdout is useful on the unix command line. It can be used just like any other unix tool, and in combination with any of them.

    See The Art of Unix programming, or any good unix book. Its not more work to learn the unix way along with the perl way - its less!

    qq

Re: Re: Code Optimization v5.8.1
by mirod (Canon) on May 20, 2004 at 20:19 UTC

    In this case I often use an alternate method, especially if there are lots of accronyms in the list: I scan the text for things that look like accronyms, and see if they exist in the hash:

    #!/usr/bin/perl -w use strict; 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", ); my $text=<<TEXT; Here is a text that includes LOTS of accronyms like HTML, ICBM, SCUBA etc. Maybe this should be a FAQ. TEXT # substitute all accronymns in the text $text=~ s{([A-Z0-9]+)} # find all upper-case words (st +ored in $1) { $acro{$1} # is it in %acro? ? "$1 ($acro{$1})" # yes, expand it : $1 # no, leave it as is }gex; # g means to do it as many time +s as possible # e means to execute the code i +n the replacement part # x allows multi-line regexp an +d comments print $text;

    Note that if accronyms can include lower case letters, you will need to change the matching regexp to include them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found