in reply to Re^2: using grep on a directory to list files for a single date
in thread using grep on a directory to list files for a single date

What is the if in the map trying to do?
if $date eq $comp is false, map adds an undef to the returned list.
if $date eq $comp is true, map returns "$_\n".
Below, I assume that you're were trying to filter out dates that don't match. Filtering is grep's job, not map's. The "empty" elements you're getting are the undef returned by map when $date eq $comp is false.

$! doesn't have any meaningful value after calling -e.

The -e is redundant. opendir will fail if the dir doesn't exist, and you already handle that.

The capture in /^(\.+?)$/ wastes time. The ? is meaningless. I wonder if $_ eq '.' || $_ eq '..' would be faster.

It's probably faster to divide $comp in $year, $month, $day than to convert all the mtimes to strings.

sub list { my ($path, $comp) = @_; $comp =~ m#^(..)/(..)/(....)$# or die("Error: Badly formatted \$comp.\n"); my $comp_d = $1; my $comp_m = $2; my $comp_y = $3; local *DIR; opendir(DIR, $path) or die("Error: Unable to open directory $path: $!\n"); my @filtered_listing; while (<DIR>) { next if /^\.+$/; my ($mtime_d, $mtime_m, $mtime_y) = (localtime( (stat "$path/$_")[9] ) )[3..5]; next unless ( $mtime_d == $comp_d && $mtime_m == $comp_m && $mtime_y == $comp_y ); push(@filtered_listing, $_); } return sort @filtered_listing; }

Replies are listed 'Best First'.
Re^4: using grep on a directory to list files for a single date
by ikegami (Patriarch) on Dec 01, 2004 at 16:25 UTC

    oh, you might want to return a reference to the array instead of the array itself, especially if it's big.

    ... return [ sort @filtered_listing ]; } my $array = list(...); foreach (0..$#$array) { ... $$array[$_] ... }