in reply to noshow by file extension

opendir(DIR, $directory) || die print "Couldn't open directory"; my @unscont = sort grep !/^\.\.?$/ && !/\.gif$/, readdir(DIR); closedir(DIR); @files = grep(/\./, @unscont); @dirs = grep(!/\./, @unscont); for my $file (@files) { print qq[ <a style="{font-size: 13px; color: #000000;"} href="./$ +this?func=read&file=$file&dire=$dire">$file</a><br>\n]; }
Note the use of grep to sort out all entries consisting of a single or double dot and those ending in .gif right off the bat, then shuffling it through sort so that you don't have to call sort twice to sort the lists of files and directories individually. You can also write that separation into @files and @dirs as (/\./ ? push @files, $_ : push @dirs, $_) for @unscont;thus finishing the job with a single pass through the list. Lastly, /\./ is a really lousy form of checking for directories because who's to say a file has to have a dot in its name, or that a directory cannot have one? Instead, use the -d operator as in
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.)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re:x2 noshow by file extension
by grinder (Bishop) on Jun 03, 2002 at 13:47 UTC
    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'
      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.