http://qs321.pair.com?node_id=32271


in reply to processing logic help

OK, this is my shot. On review, it perhaps isn't too good an approach - it generates an array of the files' serial numbers in the order they need to be processed, which I thought would be a good idea but not I'm not too sure. I've moved the routines to read/write $lastfile into read_lastkey() and write_lastkey() respectively, which is almost definitely a good idea imho. I haven't tested this code at all. It may not even compile, although I rather hope it would :)
# 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]"; }