Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
First of all, you'll want to replace that 'give me a' with a subroutine, since you call it X times. Like so:
sub give_me_a { my ($part_of_speech) = @_; print STDOUT "Give me a $part_of_speech: "; my $word = <STDIN>; chomp($word); $word; }
More importantly, there're a number of ways you can make the replace more efficient. First, there're a billion text templating modules on CPAN. Text::Template is the most commonly used; that would allow you to do variable substitution directly into the templates, like so:
The quick $adj1 fox ${verb1}ed over the lazy $noun1.
Better would be to put all of your nouns and adjectives into arrays or hash tables. Something like this would do:
my %word_list = (); foreach my $part_of_speech ( 'noun', 'verb', 'adj' ) { for (my $word_idx = 1; $word_idx <= 3; $word_idx++) { $word_list{$part_of_speech}{$word_idx} = give_me_a($part_of_spe +ech); } } foreach my $line (@story) { $line =~ s/\[(noun|verb|adj)([1-3])\]/$word_list{$1}{$2}/g; print STDOUT $line; }
I'm not sure at all if that's more elegant, but it's shorter. The completed script is here: "Mad Libs Rewrite" .


In reply to Re: An Elegance Question by stephen
in thread An Elegance Question by Simplicus

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (3)
    As of 2018-10-22 18:12 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (121 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!