in reply to Re: Get fullpath of file using grep
in thread Get fullpath of file using grep
Why chain grep() and map() if you can just use grep()?
my $dir = "/tmp"; opendir my $dh, $dir or die "opendir: $!"; my @files = grep { $_ = "$dir/$_"; -f && m/\.dat$/ } readdir($dh);
It works because grep() makes $_ an alias for a list element so it can be modified in-place. Some caution is advised however (see perlfunc entry for grep() for details).
PS This code may also be a bit more efficient as it doesn't create a copy of an array returned by readdir() while doing map() but I'm not 100% sure.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Get fullpath of file using grep
by choroba (Cardinal) on Jan 28, 2015 at 15:25 UTC | |
by jmacloue (Beadle) on Jan 29, 2015 at 15:08 UTC | |
by hotpelmen (Scribe) on Jan 30, 2015 at 15:08 UTC |