From: Anno Siegel on
it_says_BALLS_on_your forehead <simon.chao(a)fmr.com> wrote in comp.lang.perl.misc:
>
> 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}.

Why a hash? A plain array wuld serve the same purpose here.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
From: it_says_BALLS_on_your forehead on

Anno Siegel wrote:
> it_says_BALLS_on_your forehead <simon.chao(a)fmr.com> wrote in comp.lang.perl.misc:
> >
> > 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}.
>
> Why a hash? A plain array wuld serve the same purpose here.

hahaha, you're right. my first thought was to take advantage of the
fact that hashes have no inherent order, but then i remembered that the
'randomness' of the sort is locked after the hash ceases to change ( at
least, that's what i think... ), so i decided to use the OP's idea of a
random number generator. unfortunately my head was still stuck around
hashes so i used one. but arrays take less memory than hashes, and i'm
using the hash as an array, so i should have just stuck with arrays :-)

From: Tad McClellan on
ofer(a)ilunix.org <ofer(a)ilunix.org> wrote:

> 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 warnings;
use strict;

$/ = "%\n";
my @quotes = <DATA>;
print $quotes[ rand @quotes ];

__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>
-------------


--
Tad McClellan SGML consulting
tadmc(a)augustmail.com Perl programming
Fort Worth, Texas
From: ofer on
Thank you all very much! :)
I don't have the program "fortune" on my server so it didn't help.
During the night I thought about something nice ( before I saw Tad
McClellan's post ) so here it is:

#!/usr/bin/perl
# fortune cookie chooser

use warnings;
use strict;


print fortune();

sub fortune {
open( R, 'cookies.txt' ) or return 0;
# enable slurp mode:
local $/ = undef;
my $file = <R>;
my @cookies = split( /%\n/, $file );
my $rand_cookie = int( rand( $#cookies ) );
return $cookies[ $rand_cookie ];
}

From: A. Sinan Unur on
ofer(a)ilunix.org wrote in news:1140169028.364920.260650
@g14g2000cwa.googlegroups.com:

> Thank you all very much! :)
> I don't have the program "fortune" on my server so it didn't help.
> During the night I thought about something nice ( before I saw Tad
> McClellan's post )

You did not see mine either?

> #!/usr/bin/perl
> # fortune cookie chooser
>
> use warnings;
> use strict;
>
> print fortune();
>
> sub fortune {
> open( R, 'cookies.txt' ) or return 0;

You want to return a false value that works regardless of the context in
which fortune is called. There is no need to create a bareword
filehandle that is visible everywhere in your program.

open my $fortune_h, '<', 'cookies.txt' or return;

> # enable slurp mode:
> local $/ = undef;
> my $file = <R>;
> my @cookies = split( /%\n/, $file );

See
http://groups.google.com/group/comp.lang.perl.misc/msg/e5da2a1e4ce70c0d

It is much less efficient to slurp the file into a scalar and then split
than to just read it in an array using the appropriate input record
separator. (Note the chomp in my code to get rid of the %\n.)

> my $rand_cookie = int( rand( $#cookies ) );

perldoc -f rand

When you call rand(n), it returns a number between 0 and n-1. $#cookies
is already one less than the number of elements in the array. So, using
this could, you would always be ignorine the last cookie.

> return $cookies[ $rand_cookie ];

return $cookies[ rand @cookies ];

is fine. The int in my code is not necessary, although it does not hurt.

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