random fortune cookie

O

ofer

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 <[email protected]
%
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 <[email protected]>
%
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 <[email protected]>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <[email protected]>
%

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?
 
I

it_says_BALLS_on_your forehead

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 <[email protected]
%
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 <[email protected]>
%
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 <[email protected]>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <[email protected]>
%

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

J. Gleixner

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

A. Sinan Unur

(e-mail address removed) wrote in @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 <[email protected]
%
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 <[email protected]>
%
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 <[email protected]>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <[email protected]>
%

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 <[email protected]>

Sinan
 
R

Rick Scott

([email protected] 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
 
A

Anno Siegel

it_says_BALLS_on_your forehead said:
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
 
I

it_says_BALLS_on_your forehead

Anno said:
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 :)
 
T

Tad McClellan

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 <[email protected]
%
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 <[email protected]>
%
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 <[email protected]>
%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <[email protected]>
 
O

ofer

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

A. Sinan Unur

(e-mail address removed) wrote in @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
 
O

ofer

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

A. Sinan Unur

(e-mail address removed) wrote in @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.

Sinan
 
J

Josef Moellers

okay got it! thank you. works like a charm.
you can see it working in http://ilunix.org under the "user-agent"
line. :)
No, apparently you haven't "got it":

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

A. Sinan Unur

(e-mail address removed) wrote in @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:

[ 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
 
J

Josef Moellers

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
 
O

ofer

Josef said:
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 <[email protected]>
(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

A. Sinan Unur

(e-mail address removed) wrote in @g44g2000cwa.googlegroups.com:
....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top