| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/07/17 10:51:36
Modified: . perlfaq5.pod
Log:
* How do I select a random line from a file?
* changes from mjd
+ make Tie::File mention less prominent
+ cite Knuth correctly
Revision Changes Path
1.21 +6 -6 perlfaq/perlfaq5.pod
Index: perlfaq5.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -w -r1.20 -r1.21
--- perlfaq5.pod 21 Jun 2002 04:28:27 -0000 1.20
+++ perlfaq5.pod 17 Jul 2002 17:51:36 -0000 1.21
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq5 - Files and Formats ($Revision: 1.20 $, $Date: 2002/06/21 04:28:27 $)
+perlfaq5 - Files and Formats ($Revision: 1.21 $, $Date: 2002/07/17 17:51:36 $)
=head1 DESCRIPTION
@@ -970,17 +970,17 @@
=head2 How do I select a random line from a file?
-You can use the Tie::File method which treats the entire file
-as an array. Simply access a random array element.
-
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 an explanation of this method in The Art of Computer
-Programming, Volume 2, Section 3.4.2 by Donald Knuth.
+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.
+
+Another way is to use the Tie::File module, which treats the entire
+file as an array. Simply access a random array element.
=head2 Why do I get weird spaces when I print an array of lines?