Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

random fortune cookie

8 views
Skip to first unread message

of...@ilunix.org

unread,
Feb 15, 2006, 3:53:18 PM2/15/06
to
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.0...@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 <96...@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 <10...@jpl-devvax.JPL.NASA.GOV>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <1992Jul3.1...@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?

it_says_BALLS_on_your forehead

unread,
Feb 15, 2006, 4:04:33 PM2/15/06
to

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}.

J. Gleixner

unread,
Feb 15, 2006, 4:37:25 PM2/15/06
to
of...@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.

A. Sinan Unur

unread,
Feb 15, 2006, 4:45:37 PM2/15/06
to
of...@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.0...@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 <96...@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 <10...@jpl-devvax.JPL.NASA.GOV>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <1992Jul3.1...@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 <10...@jpl-devvax.JPL.NASA.GOV>

Sinan
--
A. Sinan Unur <1u...@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

Rick Scott

unread,
Feb 15, 2006, 6:16:01 PM2/15/06
to
(of...@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

Anno Siegel

unread,
Feb 16, 2006, 8:29:57 AM2/16/06
to
it_says_BALLS_on_your forehead <simon...@fmr.com> wrote in comp.lang.perl.misc:

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.

it_says_BALLS_on_your forehead

unread,
Feb 16, 2006, 8:52:52 AM2/16/06
to

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 :-)

Tad McClellan

unread,
Feb 16, 2006, 12:06:24 PM2/16/06
to
of...@ilunix.org <of...@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.0...@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 <96...@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 <10...@jpl-devvax.JPL.NASA.GOV>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <1992Jul3.1...@netlabs.com>

-------------


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

of...@ilunix.org

unread,
Feb 17, 2006, 4:37:08 AM2/17/06
to
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 ];
}

A. Sinan Unur

unread,
Feb 17, 2006, 4:52:26 AM2/17/06
to
of...@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.

of...@ilunix.org

unread,
Feb 17, 2006, 7:34:32 AM2/17/06
to
Thanks, it is always good to avoid bad programming habits :)
I didn't understand what this should do:
chomp(@quotes = <DATA>);
This does the same as:

while ( <DATA> )
chomp;
push @quotes, $_;
}

?

I've fixed everything you said beside of this because I want to
understand what have you done here first :)
Thanks a lot for your time

A. Sinan Unur

unread,
Feb 17, 2006, 7:41:46 AM2/17/06
to
of...@ilunix.org wrote in news:1140179672.675863.160700
@f14g2000cwb.googlegroups.com:

[ Please quote some context when you reply. Intersperse your comments
with the bits you are commenting on. ]

> Thanks, it is always good to avoid bad programming habits :)
> I didn't understand what this should do:
> chomp(@quotes = <DATA>);
> This does the same as:
>
> while ( <DATA> )
> chomp;
> push @quotes, $_;
> }
>
> ?

Well, it first slurps the filehandle into an array. At this point, each
element of the array is a line from the cookies.txt file (still
containing the record separator which we set to "%\n").

So, we need to get rid of those "%\n" sequences at the end.

chomp, applied to list, works element-wise. That is, it chomps every
element of the array. See perldoc -f chomp.

After

chomp(@quotes = <DATA>);

we have in @quotes each quote without the "%\n" sequence.

of...@ilunix.org

unread,
Feb 17, 2006, 9:32:04 AM2/17/06
to
okay got it! thank you. works like a charm.
you can see it working in http://ilunix.org under the "user-agent"
line. :)

Josef Moellers

unread,
Feb 17, 2006, 9:36:37 AM2/17/06
to
No, apparently you haven't "got it":

[ Please quote some context when you reply. Intersperse your comments
with the bits you are commenting on. ]


--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

of...@ilunix.org

unread,
Feb 17, 2006, 9:49:49 AM2/17/06
to
what do you mean?

A. Sinan Unur

unread,
Feb 17, 2006, 10:06:34 AM2/17/06
to
of...@ilunix.org wrote in news:1140187789.868402.43630
@g47g2000cwa.googlegroups.com:

> what do you mean?

I feel like we are collectively talking to a wall here.

I don't like talking to a wall.

If you are somehow unable to comperehend:

"A. Sinan Unur" <1u...@llenroc.ude.invalid> wrote in
news:Xns976D4E629902...@127.0.0.1:

>>> [ Please quote some context when you reply. Intersperse your comments
>>> with the bits you are commenting on. ]

then you have no business programming.

Bye.

Sinan

Josef Moellers

unread,
Feb 17, 2006, 10:14:27 AM2/17/06
to
of...@ilunix.org wrote:
> what do you mean?
>

Sinan pointed out to you that you are supposed to quote some context
when you reply. This makes it easier for others to sort out what exactly
your reply means without having to go back in the thread. Not following
this advice is considered unfriendly and will cause reluctance to help
you in the future.

Josef

of...@ilunix.org

unread,
Feb 17, 2006, 10:39:15 AM2/17/06
to

Josef Moellers wrote:
> Sinan pointed out to you that you are supposed to quote some context
> when you reply. This makes it easier for others to sort out what exactly
> your reply means without having to go back in the thread. Not following
> this advice is considered unfriendly and will cause reluctance to help

Oh, I see.
Sorry, I'm new with this Usenet thingy.
Now I've read the 'clpmisc' guidelines from Sinan's signature:
> A. Sinan Unur <1...@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

So I'll behave better next time. Sorry and thanks for your time.

A. Sinan Unur

unread,
Feb 17, 2006, 1:28:14 PM2/17/06
to
of...@ilunix.org wrote in news:1140190755.869419.8000
@g44g2000cwa.googlegroups.com:

>
> Josef Moellers wrote:
>> Sinan pointed out to you that you are supposed to quote some context
>> when you reply.

...

> Oh, I see. Sorry, I'm new with this Usenet thingy.
> Now I've read the 'clpmisc' guidelines

...

> thanks for your time.


You are welcome and your efforts are much appreciated.

Sinan

Samwyse

unread,
Feb 17, 2006, 7:39:39 PM2/17/06
to
of...@ilunix.org wrote:
> what do you mean?

What do *you* mean? Is this question directed to someone? What are you
refering to? Can you provide some context?

0 new messages