in reply to RE: Re: Checking for Directories
in thread Checking for Directories

If the debugger is accurate, you are stepping through the array three times to produce your results. If you have a directory with a large number of files in it, this is not the most runtime-efficient way of doing it.

This version makes a single pass through the directory listing.

#Assume all the open code foreach( readdir DH ) { next if ( $_ eq '.' or $_ eq '..' ); push @subdirs, "$dir/$_" if ( -d "$dir/$_" ); }
Of course, you may still not like this. It will be kinda hungry on memory, especially if the ratio of files to directories is very large - the foreach will suck in every file at once. It may be better to try it with a while loop - it will be easier on memory but will take longer due to buffering issues.
mikfire

Replies are listed 'Best First'.
Checking for Directories
by chip (Curate) on Jul 29, 2000 at 00:30 UTC
    Unless you've run it under Benchmark, you don't know whether your intuition is correct about which approach is faster. Besides, fewer lines of code and fewer variable names makes for easier maintenance, all things equal.

        -- Chip Salzenberg, Free-Floating Agent of Chaos