Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Random word generator

by McDarren (Abbot)
on Feb 27, 2008 at 14:29 UTC ( [id://670642]=note: print w/replies, xml ) Need Help??


in reply to Random word generator

I'm not familiar with solaris, but as it's a *nix system I'd imagine you'll find a wordlist in somewhere like /usr/dict/words.

Using this as a base, it's fairly trivial to generate random words. You can restrict the length with the built in length function.

Alternatively, there is always CPAN - Data::Random::WordList, for example. You could use that to generate the words, and again use length to throw away the ones that are not the right length.

Update: And for no other reason than I felt like it ;), here is a short snippet of code that demonstrates how one might do this (in this case, using shuffle() from List::Util):

#!/usr/bin/perl -l use strict; use warnings; use List::Util qw/shuffle/; my $wordlist = '/usr/share/dict/words'; my $length = 10; my $numwords = 10; my @words; open WORDS, '<', $wordlist or die "Cannot open $wordlist:$!"; while (my $word = <WORDS>) { chomp($word); push @words, $word if (length($word) == $length); } close WORDS; my @shuffled_words = shuffle(@words); print for @shuffled_words[0 .. 9];

Hope this helps,
Darren :)

Replies are listed 'Best First'.
Re^2: Random word generator
by rooneyl (Sexton) on Feb 27, 2008 at 19:27 UTC
    Thank you for your help. Much appreciated. I like the List::Util version, the Data:Random:wordlist isn't that good at returning words of a given length. I needed to get a large list to ensure a number of words of the length required.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://670642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found