I have logfiles that are named elmo{x}.txt where x is 0000..9999. these are delivered by rsync, when the count goes to 9999 it goes over the top to 0000.
I have to process them in order and only once. I save the last number to a file and try and proceed from there.
These files arrive in sequence at random intervals but up to about 15 at a time.

The code I have come up with is below but is rather kludgy. Can anyone suggest improvements to the logic please??
cheers

my %temp = (); my %files = (); my ($zeroes, $nines) = (0,0); my $lastkey = 0; # lastfile contains number open (QS, $lastfile) or die "cant open $lastfile $!"; while (<QS>) { $lastkey = $_; } close QS; # <> contains the list of files from a .sh while (<>) { chop; next if $_ !~ /\.txt$/; my $file = $indir.$_; $_ =~ s/elmo(\d*)\.txt$/$1/; $temp{$_} = [ $file, $_ ]; $nines = 1 if $_ =~ /^999/; $zeroes = 1 if $_ =~ /^000/; } for (keys %temp) { my $k = $_; if (($nines && $zeroes) || ($zeroes && $lastkey =~ /^99/)) { $k += 10000 if ($k =~ /^000/); $lastkey += 10000 if ($lastkey =~ /^00/ && $lastkey < 9999); } $files{$k} = [ @{ $temp{$_} }]; } for (sort {$a <=> $b} keys %files) { my $file = $files{$_}->[0]; my $numkey = $_; if ($numkey > $lastkey) { if (-e $file){ # &io_process_file($lda, $file, 'incstock', \&load_incstock, + 1); $lastkey = $numkey; $lastkey += 10000 if (($nines && $zeroes) && $lastkey < 900 +0); } } } open (QS, ">$lastfile") or die "cant open $lastfile $!"; print QS $files{$lastkey}->[1]; close QS;

In reply to processing logic help by agoth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.