# Pass list of filenames as arguments sub listnumbers { my %files; foreach (@_) { next unless (/^elmo(\d+)\.txt$/); my $n = int($1); # Force string to integer $files{$n} = $_; } # Did we wrap-around? This happens iff we have a 0 and a # 9999 if (exists($files{0}) && exists($files{9999})) { # OK, so this is the tricky case # Count down from 9999 to find the first file my $lwm = 9999; while (exists($files{$lwm})) { --$lwm; die "I think something broke" if ($lwm < 5000); } # And count up from 0 to get the last file my $hwm = 0; while (exists($files{$hwm})) { ++$hwm; die "I think something broke" if ($hwm > 5000); } # And return the intervals ($lwm+1..9999, 0..$hwm-1); } else { # Easy case sort keys %files; } } # Main code my @numbers = listnumbers(<>); my $lastkey = read_lastkey(); my $foundlast = 0; foreach (@numbers) { if ($_ == $lastkey) { $foundlast = 1; next; } if ((($_ + 1) % 10000) == $lastkey) { $foundlast = 1; } if ($foundlast) { process_file(sprintf("elmo%04d.txt", $_)); $lastkey = $_; } } if ($foundlast) { write_lastkey($lastkey); } else { die "Didn't find a plausible sequel to $lastkey in [@numbers]"; }