|
From: ofer on 15 Feb 2006 15:53 Hi, I got a file with many quotations by Larry Wall. The file uses the fortune cookie format: All language designers are arrogant. Goes with the territory... :-) -- Larry Wall in <1991Jul13.010945.19157(a)netlabs.com % Although the Perl Slogan is There's More Than One Way to Do It, I hesitate to make 10 ways to do something. :-) -- Larry Wall in <9695(a)jpl-devvax.JPL.NASA.GOV> % And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is. :-) -- Larry Wall in <10209(a)jpl-devvax.JPL.NASA.GOV> % "And I don't like doing silly things (except on purpose)." -- Larry Wall in <1992Jul3.191825.14435(a)netlabs.com> % Each time when a visitor comes visit me in my website, I want him to view a quotation by Larry Wall, and I want it to be a random one. I have an idea about how to do it, but I think it sucks: 1. Get this file to memory using slurp ( the file is 60kb ) 2. count how many times we have "\n%\n" in the file 3. generate a random number that will fit the results from ( 2 ) 4. count "\n%\n" until we get to the random number 5. take the quote from there It looks pretty bad, I know. Got any other idea for me how to do it?
From: it_says_BALLS_on_your forehead on 15 Feb 2006 16:04 ofer(a)ilunix.org wrote: > Hi, I got a file with many quotations by Larry Wall. > The file uses the fortune cookie format: > > All language designers are arrogant. Goes with the territory... :-) > -- Larry Wall in <1991Jul13.010945.19157(a)netlabs.com > % > Although the Perl Slogan is There's More Than One Way to Do It, I > hesitate > to make 10 ways to do something. :-) > -- Larry Wall in <9695(a)jpl-devvax.JPL.NASA.GOV> > % > And don't tell me there isn't one bit of difference between null and > space, > because that's exactly how much difference there is. :-) > -- Larry Wall in <10209(a)jpl-devvax.JPL.NASA.GOV> > % > "And I don't like doing silly things (except on purpose)." > -- Larry Wall in <1992Jul3.191825.14435(a)netlabs.com> > % > > Each time when a visitor comes visit me in my website, I want him to > view a quotation by Larry Wall, and I want it to be a random one. > I have an idea about how to do it, but I think it sucks: > 1. Get this file to memory using slurp ( the file is 60kb ) > 2. count how many times we have "\n%\n" in the file > 3. generate a random number that will fit the results from ( 2 ) > 4. count "\n%\n" until we get to the random number > 5. take the quote from there > > It looks pretty bad, I know. > Got any other idea for me how to do it? read each line into a hash with the line number as the key, and the quote as the value. get a random number, mod it by keys %hash (in scalar context). print $hash{$number}.
From: J. Gleixner on 15 Feb 2006 16:37 ofer(a)ilunix.org wrote: > Hi, I got a file with many quotations by Larry Wall. > Each time when a visitor comes visit me in my website, I want him to > view a quotation by Larry Wall, and I want it to be a random one. > Got any other idea for me how to do it? Maybe it's already an FAQ? perldoc -q "How do I select a random line from a file" Also look at $/ and perldoc -f chomp. Post your code, if you have questions.
From: A. Sinan Unur on 15 Feb 2006 16:45 ofer(a)ilunix.org wrote in news:1140036798.158996.97960 @o13g2000cwo.googlegroups.com: > Hi, I got a file with many quotations by Larry Wall. > The file uses the fortune cookie format: .... > Each time when a visitor comes visit me in my website, I want him to > view a quotation by Larry Wall, and I want it to be a random one. .... > Got any other idea for me how to do it? #!/usr/bin/perl use strict; use warnings; my @quotes; { local $/ = "%\n"; chomp(@quotes = <DATA>); } print $quotes[int(rand @quotes)], "\n"; __DATA__ All language designers are arrogant. Goes with the territory... :-) -- Larry Wall in <1991Jul13.010945.19157(a)netlabs.com % Although the Perl Slogan is There's More Than One Way to Do It, I hesitate to make 10 ways to do something. :-) -- Larry Wall in <9695(a)jpl-devvax.JPL.NASA.GOV> % And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is. :-) -- Larry Wall in <10209(a)jpl-devvax.JPL.NASA.GOV> % "And I don't like doing silly things (except on purpose)." -- Larry Wall in <1992Jul3.191825.14435(a)netlabs.com> % D:\Home\asu1\UseNet\clpmisc> fortune.pl And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is. :-) -- Larry Wall in <10209(a)jpl-devvax.JPL.NASA.GOV> Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
From: Rick Scott on 15 Feb 2006 18:16 (ofer(a)ilunix.org uttered:) > Hi, I got a file with many quotations by Larry Wall. > The file uses the fortune cookie format: > ... > Each time when a visitor comes visit me in my website, I want him to > view a quotation by Larry Wall, and I want it to be a random one. > I have an idea about how to do it, but I think it sucks: > 1. Get this file to memory using slurp ( the file is 60kb ) > 2. count how many times we have "\n%\n" in the file > 3. generate a random number that will fit the results from ( 2 ) > 4. count "\n%\n" until we get to the random number > 5. take the quote from there > > It looks pretty bad, I know. > Got any other idea for me how to do it? my $fortune = `/usr/games/fortune larry_wall_quotes.txt`; Rick -- key CF8F8A75 / print C5C1 F87D 5056 D2C0 D5CE D58F 970F 04D1 CF8F 8A75 To thine own self be true, then to no man can you be false. :William Shakespeare
|
Next
|
Last
Pages: 1 2 3 4 Prev: Can I Grep multiple lines?? Next: Problem reading & displaying MHTML file type |