[alt.sources.wanted] Perl quote printer

merlyn@iwarp.intel.com (Randal Schwartz) (12/22/89)

In article <1989Dec19.181040.1554@tvcent.uucp>, andrew@tvcent (Andrew Cowie) writes:
| In article <1989Dec16.064319.21721@agate.berkeley.edu> c60c-4ay@e260-1b (Raja S Kushalnagar) writes:
| >  (lines deleted)
| >and so on.  I could write a C program to print a quote at random.  But I was
| >wondering if it could be done with Unix commands or awk alone.  Could it?
| 
| Yes. A while ago a small script was posted that got a random quote
| from a file of star trek quotes. Here is the script:
| 
| --------
| #
| # fortune.sh - uses $$ for rand.
| #
| a=`expr $$ % 400`
| expr "     $a" : '.*\(.....\)'  | \
| join -t% -o 2.2 - $HOME/bin/stquotes
| --------
| 
| It operates on a file of the following format: (there were 400 originally,
| I figured that you didn't need the whole file :-)
| 
| --------
|     0%Star Trek Lives!
|     1%Schshschshchsch.
|     1%    -- The Gorn, "Arena," stardate 3046.2.
|     2%Live long and prosper.
|     2%    -- Spock, "Amok Time," stardate 3372.7.
|     3%Totally illogical, there was no chance.
|     3%    -- Spock, "The Galileo Seven," stardate 2822.3.
|     4%All your people must learn before you can reach for the stars.
|     4%    -- Kirk, "The Gamesters of Triskelion," stardate 3259.2.
|     5%We have found all life forms in the galaxy are capable of
|     5%superior development.
|     5%    -- Kirk, "The Gamesters of Triskelion," stardate 3211.7.
| --------
| 
| Note that quotes can be of any length. I hope this works for what you are
| trying to do.

Yeah, but you forgot the Perl version (which I didn't save, but
am recreating from scratch)...

#!/usr/bin/perl
@quotes = split(/\n/, <<'END_OF_QUOTES');
     0%Star Trek Lives!
     1%Schshschshchsch.
     1%    -- The Gorn, "Arena," stardate 3046.2.
     2%Live long and prosper.
     2%    -- Spock, "Amok Time," stardate 3372.7.
     3%Totally illogical, there was no chance.
     3%    -- Spock, "The Galileo Seven," stardate 2822.3.
     4%All your people must learn before you can reach for the stars.
     4%    -- Kirk, "The Gamesters of Triskelion," stardate 3259.2.
     5%We have found all life forms in the galaxy are capable of
     5%superior development.
     5%    -- Kirk, "The Gamesters of Triskelion," stardate 3211.7.
END_OF_QUOTES
($maxq = $quotes[$#quotes]) =~ s/^\s*(\d+)%.*/$1/; # get highest number
srand; $randq = int(rand($maxq+1)); # select random quote
print grep(s/^\s*$randq%(.*)/$1\n/o,@quotes); # extract/print from array

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

lwall@jato.Jpl.Nasa.Gov (Larry Wall) (12/28/89)

In article <5378@omepd.UUCP> merlyn@iwarp.intel.com (Randal Schwartz) writes:
: Yeah, but you forgot the Perl version (which I didn't save, but
: am recreating from scratch)...
: 
: #!/usr/bin/perl
: @quotes = split(/\n/, <<'END_OF_QUOTES');
:      0%Star Trek Lives!
:      1%Schshschshchsch.
:      1%    -- The Gorn, "Arena," stardate 3046.2.
:      2%Live long and prosper.
:      2%    -- Spock, "Amok Time," stardate 3372.7.
:      3%Totally illogical, there was no chance.
:      3%    -- Spock, "The Galileo Seven," stardate 2822.3.
:      4%All your people must learn before you can reach for the stars.
:      4%    -- Kirk, "The Gamesters of Triskelion," stardate 3259.2.
:      5%We have found all life forms in the galaxy are capable of
:      5%superior development.
:      5%    -- Kirk, "The Gamesters of Triskelion," stardate 3211.7.
: END_OF_QUOTES
: ($maxq = $quotes[$#quotes]) =~ s/^\s*(\d+)%.*/$1/; # get highest number
: srand; $randq = int(rand($maxq+1)); # select random quote
: print grep(s/^\s*$randq%(.*)/$1\n/o,@quotes); # extract/print from array
: 
: Just another Perl hacker,
: /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\

There's no reason to force the data-entry operator to type in all these
arbitrary numbers.  The data format is also virtually unreadable.  The
algorithm could use simplification.  I'd suggest the following instead:

#!/usr/bin/perl
@quotes = split(/\n\n/, <<'END_OF_QUOTES');
Star Trek Lives!

Schshschshchsch.
    -- The Gorn, "Arena," stardate 3046.2.

Live long and prosper.
    -- Spock, "Amok Time," stardate 3372.7.

Totally illogical, there was no chance.
    -- Spock, "The Galileo Seven," stardate 2822.3.

All your people must learn before you can reach for the stars.
    -- Kirk, "The Gamesters of Triskelion," stardate 3259.2.

We have found all life forms in the galaxy are capable of
superior development.
    -- Kirk, "The Gamesters of Triskelion," stardate 3211.7.
END_OF_QUOTES
srand; print $quotes[int(rand($#quotes+1))],"\n"; # print random quote

If you want a quote with embedded newline pairs, just type a space between
them.

If you want to have an external data file called "quotes", just say something
like:

#!/usr/bin/perl
$/ = '';				# use paragraph mode
open(QUOTES,'quotes') || die "Can't open my precious quotes file: $!";
@quotes = <QUOTES>;			# read paragraphs into array
srand; print $quotes[int(rand($#quotes+1))]; # print random quote

Or, for those who like "one-line" solutions:

#!/usr/bin/perl
$/='';open(Q,'quotes')||die"phooey";@q=<Q>;srand;print$q[int(rand($#q+1))];

Not just another perl hacker,		:-)
Larry Wall
lwall@jpl-devvax.jpl.nasa.gov
"So many programs, so little time..."