in reply to Re: noshow by file extension
in thread noshow by file extension

my @unscont = sort grep !/^\.\.?$/ && !/\.gif$/, readdir(DIR);

Bad advice. If that grep !/^\.\.?$/ is there to remove the current and parent directories, it won't always work.

Trying to remove these directories from the output of readdir with a regexp is hard to get right (and when you get it right it's damned ugly). Use procedural code instead. It's clearer and has no borderline cases to trip you up.

my @unscont = sort grep { $_ ne '.' and $_ ne '..' and !/\.gif$/ } readdir(DIR);
my @dirs = grep -d "$directory/$_", @unscont; my @files = grep ! -d "$directory/$_", @unscont;
(Or as part of my push proposition; that shall be left as an excercise to the reader.)

If you want to separate files and dirs in one pass, you could do something like:

my( @dirs, @files ); push @{ -d "$directory/$_" ? \@dirs : \@files}, $_ for @unscont;

That is, I could admit to doing that, but I'm not sure that it's crystal clear in its intent :)


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Replies are listed 'Best First'.
Re^3: noshow by file extension
by Aristotle (Chancellor) on Jun 03, 2002 at 21:47 UTC
    Thanks for the corrections - esp for the grep-regex pitfall. Duh at myself for the push bit (putting it in mere round brackets and returning simple arrays doesn't work; should have thought of return refs and dereferencing them). Yes, it's a bit unclear, but esp when we're talking about a CGI that may have to handle large directories and somewhat substantial traffic I'd prefer to use it this and stick a comment in to explain it instead.

    Makeshifts last the longest.