Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Answer: How do I pick a random line from a file?

by vroom (Pope)
on Jan 09, 2000 at 11:00 UTC ( #1913=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How do I pick a random line from a file? - Answer contributed by vroom

Another more obvious way, but possibly memory intensive way would be to read the entire file into an array and then pick a random element.
open FILE, "<$file" or die "Could not open $file: $!\n"; @array=<FILE>; close FILE; $randomline=$array[rand @array];

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-12-15 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?