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]";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.