| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/09/04 15:39:30
Modified: . perlfaq5.pod
Log:
* How do I select a random line from a file?
added mention of File::Random
Revision Changes Path
1.24 +7 -1 perlfaq/perlfaq5.pod
Index: perlfaq5.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -w -r1.23 -r1.24
--- perlfaq5.pod 25 Aug 2002 20:10:29 -0000 1.23
+++ perlfaq5.pod 4 Sep 2002 22:39:29 -0000 1.24
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq5 - Files and Formats ($Revision: 1.23 $, $Date: 2002/08/25 20:10:29 $)
+perlfaq5 - Files and Formats ($Revision: 1.24 $, $Date: 2002/09/04 22:39:29 $)
=head1 DESCRIPTION
@@ -986,6 +986,12 @@
This has a significant advantage in space over reading the whole file
in. You can find a proof of this method in I<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.