Simplicus has asked for the wisdom of the Perl Monks concerning the following question:
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:
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#!/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 $_; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: An Elegance Question
by plaid (Chaplain) on Apr 13, 2000 at 23:02 UTC | |
by Simplicus (Monk) on Apr 13, 2000 at 23:07 UTC | |
by Simplicus (Monk) on Apr 13, 2000 at 23:34 UTC | |
Re: An Elegance Question
by btrott (Parson) on Apr 13, 2000 at 22:03 UTC | |
by Simplicus (Monk) on Apr 13, 2000 at 22:15 UTC | |
Re: An Elegance Question
by plaid (Chaplain) on Apr 13, 2000 at 22:30 UTC | |
by Simplicus (Monk) on Apr 13, 2000 at 22:39 UTC | |
Re: An Elegance Question
by stephen (Priest) on Apr 13, 2000 at 22:35 UTC | |
Re: An Elegance Question
by chromatic (Archbishop) on Apr 13, 2000 at 23:12 UTC | |
Re: An Elegance Question
by turnstep (Parson) on Apr 13, 2000 at 22:40 UTC | |
by Simplicus (Monk) on Apr 13, 2000 at 22:53 UTC | |
Re: An Elegance Question
by jbert (Priest) on Apr 14, 2000 at 18:55 UTC | |
Re: An Elegance Question
by Simplicus (Monk) on Apr 13, 2000 at 23:05 UTC |
Back to
Seekers of Perl Wisdom