Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!jato!lwall From: lwall@jato.Jpl.Nasa.Gov (Larry Wall) Newsgroups: alt.sources.wanted Subject: Re: Perl quote printer (was Re: Printing of a random quote.) Message-ID: <2455@jato.Jpl.Nasa.Gov> Date: 28 Dec 89 02:53:46 GMT References: <1989Dec16.064319.21721@agate.berkeley.edu> <1989Dec19.181040.1554@tvcent.uucp> <5378@omepd.UUCP> Reply-To: lwall@jato.Jpl.Nasa.Gov (Larry Wall) Distribution: na Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 73 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 = ; # 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=;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..."