Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Find images regardless of filetype extension.

by Chady (Priest)
on Aug 01, 2005 at 07:48 UTC ( [id://479822]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Find images regardless of filetype extension.
in thread Find images regardless of filetype extension.

I was aware that there had to be a better way to end up with both @files, @dirs with one test but was and still am unsure how to do it. What do you suggest? Really... I was hopping for some code suggestions/improvements

here's one way to do it:

# your version # my @tmp = readdir(DIR); # my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/$_" } +@tmp; # my @dirs = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -d "$dir/$_" } +@tmp; # another version my @files; my @dirs; my @tmp = grep { ! /^\./ } readdir DIR; # grep once. foreach (@tmp) { if ( -d "$dir/$_" ) { # stat once. push @dirs, "$dir/$_"; } else { push @files, "$dir/$_"; } }

He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
Chady | http://chady.net/
Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.

Replies are listed 'Best First'.
Re^6: Find images regardless of filetype extension.
by zzspectrez (Hermit) on Aug 01, 2005 at 14:15 UTC

    When I was writing this, I started off just looking for files in the current directory. So,  my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/$_" } was nice and compact and seemed very perlish.

    I then decided to allow directory traversal and kept the same code format. Still nice and compact, only 3 lines compared to 10 in your method. Of course, mine is obviously way less efficient.

    I can't think of a way to eliminate the extra grep and stat and keep the same compactness.

    Thanks for you suggestion.

    zzSPECTREz

    please read my disclaimer

      Compactness affects readability. And besides, you're counting declarations and braces as lines, if I put it on one line it doesn't mean it's a single statement... if you're looking for compactness, here goes:

      I'll assume @files and @dirs are already declared then.
      map {$d="$dir/$_";$r= -d $d?\@dirs:\@files;push @$r,$d;} grep {!/^\./} + readdir DIR;

      Yes, this uses map in void context, but you want compactness, not efficiency :)


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
      Chady | http://chady.net/
      Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.

        Ill give you that is compact!

        But I think, that

        • my @files = map { "$dir/$_" } grep { !/^\.{1,2}\z/ && -f "$dir/$_" } @tmp;
        is both compact but very clear and readable. On the otherhand,
        • map {$d="$dir/$_";$r= -d $d?\@dirs:\@files;push @$r,$d;} grep {!/^\./} readdir DIR;
        is compact and clever but not easily readable IMHO.

        On the other hand, I really like your use of the ternary and references. I would not have thought of using it like that. ++ ++ I think that is quite clever. Thanks for the snippet of code.

        Only one complaint, the way you wrote you regex it will skip any dot file. So .my-file would not be picked up.

        zzSPECTREz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-19 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found