#!/usr/bin/perl -w # generate_sorted_lines.pl - Don Yang (uguu.org) # # Generate a sorted file, inserting a few random lines each time. # # 06/29/11 use strict; use constant FILE_SIZE => 10000; # Build snapshot from a list of lines sub BuildSnapshot($) { my ($lines) = @_; my $text = ""; for(my $i = 0; $i <= $#$lines; $i++) { $text .= "L" . ($i + 1) . "E" . (scalar @$lines) . "=" . $$lines[$i] . "\n"; } return $text; } # Construct final file output state my @lines = (); for(my $i = 0; $i < FILE_SIZE; $i++) { push @lines, $i; } # Build file snapshots from final state to initial state, removing a # few lines each time. my @snapshots = (BuildSnapshot(\@lines)); while( scalar @lines ) { my $length = int(rand(scalar(@lines) / 100)) + 1; my $offset = int(rand(scalar(@lines) - $length)); splice @lines, $offset, $length; push @snapshots, BuildSnapshot(\@lines); } # Output snapshots in reverse order my $frame = 0; for(my $i = $#snapshots; $i > 0; $i--) { print "Y1X1F${frame}T", int($frame / 100), "\n", $snapshots[$i]; $frame++; }