CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How do I select a random line from a file?

 



Jasmine
Administrator

Jan 26, 2001, 10:43 AM

Post #1 of 1 (1657 views)
How do I select a random line from a file? Can't Post

(From the Perl FAQ)

How do I select a random line from a file?

Here's an algorithm from the Camel Book:

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

This has a significant advantage in space over reading the whole file in. A simple proof by induction is available upon request if you doubt its correctness.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives