# Sorts files in $dir and sets $latest->{file} with newest file. my $latest = (sort {$b->{mtime} <=> $a->{mtime}} map {{mtime => -M $_, file => $_}} <$dir/*>)[-1]; my $newM = (stat $latest->{file})[9]; # Sorts files in $dir and sets $oldest->{file} with oldest file. my $oldest = (sort {$a->{mtime} <=> $b->{mtime}} map {{mtime => -M $_, file => $_}} <$dir/*>)[-1]; my $oldM = (stat $oldest->{file})[9];

Why sort the same data twice?

# Sorts files in $dir and sets $latest->{file} with newest file and se +ts $oldest->{file} with oldest file. my ( $latest, $oldest ) = ( sort { $a->{ mtime } <=> $b->{ mtime } } map { { mtime => -M $_, file => $_ } } <$dir/*> )[ 0, -1 ]; my ( $newM, $oldM ) = map +( stat )[ 9 ], $latest->{ file }, $oldest-> +{ file };


# Opens $dir and counts number of files opendir(DIR, $dir); LINE: while(my $FILE = readdir(DIR)) { next LINE if($FILE =~ /^\.\.?/); $fCount++; } closedir(DIR); # Opens $dirL and counts number of files opendir(DIRL, $dirL); LINE: while(my $FILE2 = readdir(DIRL)) { next LINE if($FILE2 =~ /^\.\.?/); $fCountL++; } closedir(DIRL);

You should always verify that opendir worked correctly.    And, you don't include the directory name in your regular expression test so you are testing the wrong files and you can use grep to get your counts:

# Opens $dir and counts number of files opendir my $DIR, $dir or die "Cannot opendir '$dir' because: $!"; my $fCount = grep "$dir/$_" !~ /\A\.\.?\z/, readdir $DIR; closedir $DIR; # Opens $dirL and counts number of files opendir my $DIRL, $dirL or die "Cannot opendir '$dirL' because: $!"; my $fCountL = grep "$dirL/$_" !~ /\A\.\.?\z/, readdir $DIRL; closedir $DIRL;

In reply to Re: Looking for some assistance in cleaning up a perl script by jwkrahn
in thread Looking for some assistance in cleaning up a perl script by shadowfox

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.