Recipe 8.7 Randomizing All Lines

8.7.1 Problem

You want to copy a file and randomly reorder its lines.

8.7.2 Solution

Read all lines into an array, shuffle the array using List::Util's shuffle function, and write the shuffled lines back out:

use List::Util qw(shuffle);

while (<INPUT>) {
    push(@lines, $_);
@lines = shuffle(@lines);
foreach (@reordered) {
    print OUTPUT $_;

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 seeking 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.

8.7.4 See Also

The documentation for the standard List::Util module; Recipe 2.6; Recipe 2.7; Recipe 4.18