# 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |