Click here to get back home

FAQ 5.38 How do I select a random line from a file?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.38 How do I select a random line from a file? PerlFAQ Server 03-14-2008
Posted by PerlFAQ Server on March 14, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

5.38: How do I select a random line from a file?


Here's an algorithm from the Camel Book:

srand;
rand($.) < 1 && ($line = $_) while <>;

This has a significant advantage in space over reading the whole file
in. You can find a proof of this method in *The Art of Computer
Programming*, Volume 2, Section 3.4.2, by Donald E. Knuth.

You can use the File::Random module which provides a function for that
algorithm:

use File::Random qw/random_line/;
my $line = random_line($filename);

Another way is to use the Tie::File module, which treats the entire file
as an array. Simply access a random array element.



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 5.36: How do I select a random line from a file? November 6, 2004, 12:03 am
FAQ 5.36: How do I select a random line from a file? November 22, 2004, 6:03 pm
FAQ 5.36 How do I select a random line from a file? February 16, 2005, 6:03 pm
FAQ 5.37 How do I select a random line from a file? April 14, 2005, 11:03 am
FAQ 5.37 How do I select a random line from a file? June 30, 2005, 4:03 pm
FAQ 5.37 How do I select a random line from a file? September 14, 2005, 10:03 am
FAQ 5.37 How do I select a random line from a file? December 5, 2005, 11:03 am
FAQ 5.37 How do I select a random line from a file? August 26, 2006, 9:03 am
FAQ 5.37 How do I select a random line from a file? November 3, 2006, 3:03 am
FAQ 5.37 How do I select a random line from a file? March 1, 2007, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap