Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!zaphod.mps.ohio-state.edu!mips!apple!oliveb!orc!mipos3!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: alt.sources.wanted Subject: Perl quote printer (was Re: Printing of a random quote.) Message-ID: <5378@omepd.UUCP> Date: 22 Dec 89 01:10:07 GMT References: <1989Dec16.064319.21721@agate.berkeley.edu> <1989Dec19.181040.1554@tvcent.uucp> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: na Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 67 In-reply-to: andrew@tvcent.uucp (Andrew Cowie) 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!" ==/