Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How can I readdir and ! -d in one line

by Fletch (Bishop)
on Oct 27, 2020 at 16:14 UTC ( [id://11123219]=note: print w/replies, xml ) Need Help??


in reply to How can I readdir and ! -d in one line

grep can take a BLOCK with multiple tests and/or you can just use and and chain multiple checks (recommended best practice is to use the BLOCK form rather than the EXPR form these days anyhoo). Or use something like File::Find::Rule rather than lower level primitives.

my @F = grep { /^\w/ and not -d "$myDir/$_" } readdir( I );

Edit: Path::Tiny is another possibility.

use Path::Tiny; my @files = grep !$_->is_dir, path( $myDir )->children( qr/^\w/ );

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: How can I readdir and ! -d in one line
by misterperl (Pilgrim) on Oct 27, 2020 at 17:22 UTC
    Ahh well crafted my friend, another ++ vote.. It occurs to be in linux I might PIPE the result to ! -d or something , but I don't know about any Perl pipe.

    TYVM

      A perl equivalent (of sorts) might be to chain greps on different conditionals, but (IMHO) that's not particularly clearer in this case than simply having the one predicate step check both conditions (not-dir and starts with a wordchar) at the same time.

      my @files = grep { ! -d qq{$myDir/$_} } grep { /^\w/ } readdir( I );

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123219]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found