home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Perl CookbookPerl CookbookSearch this book

8.7. Randomizing All Lines

8.7.3. Discussion

The easiest approach is to read all lines into memory and shuffle them there. Because you don't know where lines start in the file, you can't just shuffle a list of line numbers and then extract lines in the order they'll appear in the shuffled file. Even if you did know the byte offsets of the start of each line, it would probably still be slower because you'd be seek ing around in the file instead of sequentially reading it from start to finish.

If you have a version of Perl older than v5.8, you can download the List::Util module from CPAN.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.