I have a short script that implements "Mad Libs." The idea is to read in a file containing noun1, noun2 type tags, and replace the tags with particles supplied by the user at run time. here is the script:
#!/usr/bin/perl -w
@story = <>; #filename supplied in ARGV
print "give me a noun: "; chomp($noun1 = <STDIN>);
print "give me a noun: "; chomp($noun2 = <STDIN>);
print "give me a noun: "; chomp($noun3 = <STDIN>);
print "give me a verb: "; chomp($verb1 = <STDIN>);
print "give me a verb: "; chomp($verb2 = <STDIN>);
print "give me a verb: "; chomp($verb3 = <STDIN>);
print "give me an adjective: "; chomp($adj1 = <STDIN>);
print "give me an adjective: "; chomp($adj2 = <STDIN>);
print "give me an adjective: "; chomp($adj3 = <STDIN>);
foreach (@story) {
$_ =~ s/\[noun1\]/$noun1/g;
$_ =~ s/\[noun2\]/$noun2/g;
$_ =~ s/\[noun3\]/$noun3/g;
$_ =~ s/\[verb1\]/$verb1/g;
$_ =~ s/\[verb2\]/$verb2/g;
$_ =~ s/\[verb3\]/$verb3/g;
$_ =~ s/\[adj1\]/$adj1/g;
$_ =~ s/\[adj2\]/$adj2/g;
$_ =~ s/\[adj3\]/$adj3/g;
print $_;
}
As you can see, the script reads in a story from a file, and replaces up to three instances of nouns, verbs, and adjectives. The problem is that the method is grossly inelegant. I know there must be a less brutish solution, so I turn to you for guidance. Thanks
-
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.
|