my @files = ; ## Convert the list to numbers and warn about any unusual elmos: @files = map { m/^elmo(\d{4})\.txt$/ and $1 or die "What is $_ doing here?!\n"; } @files; ## Now figure out where the largest gap is: my ($diff,$old,$high); for (sort @files) { $diff=$_-$old and $high=$_ if $_-$old>$diff; $old=$_; } ## Now go in order, starting at the first value after the gap: for ( map {$_->[0]} sort {$a->[1] <=> $b->[1] } map {[$_, $_ >= $high ? $_-10000 : $_]} @files) { print "Parsing elmo$_.txt\n"; }