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?
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}.
> 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.
> 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
> 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
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.
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 :-)
> 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
#!/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 ];
}
> 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.
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
[ 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.
[ 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
> 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
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
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.
>
> 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
What do *you* mean? Is this question directed to someone? What are you
refering to? Can you provide some context?