I think the approach went astray before line 1 and the logic looks to be in reverse order from what I'd have done.

A sensible first step (suggested in what is left hanging at the end of your code) is to convert the current time to a format that matches the directories, before doing anything with them at all (i.e. why read them into an array unfiltered - put all the filtering in one place and process in a grep/readdir loop without needing an array...)

Update: added comments in Filter subroutine as requested by OPer

# convert current time to same format as dirs my $now; { my @now = localtime(); $now = $now[5]+1900; $now[4]++; for ( my $i = 4; $i > 1; $i-- ) { $now .= '_'; $now[$i] = "0$now[$i]"; $now[$i] = substr( $now[$i], -2 ); $now .= $now[$i]; } } # get only those directories that match requirements: my $dir = "parent-of-directories"; opendir my $dh, $dir or die $!; for my $file ( grep Filter( now => $now, dir => $dir ), readdir $dh ) +{ my $path = "$dir/$file"; # and go right ahead and process the directory "$path" here in the +loop # ... # } closedir $dh; sub Filter { # put all filter conditions here ... my %opt = @_; ( -d "$opt{ dir }/$_" ) # is a directory && !/^\./ # is also not hidden/special && /(\d{4}_\d{2}_\d{2}_\d{2})/ # contains date/hour && ($opt{ now } gt $1 ) # that is in the past ; }
__________________________________________________________________________________

^M Free your mind!


In reply to Re: Appropriately assigning times to variables by Moron
in thread Appropriately assigning times to variables by Win

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.