Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Directory Listing

by elusion (Curate)
on Jul 29, 2000 at 23:12 UTC ( [id://25107]=perlquestion: print w/replies, xml ) Need Help??

elusion has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get a list of the current directories using Win98. Here's what I have:
opendir THISDIR, "." or die "serious dainbramagae: $!"; @allfiles = readdir THISDIR; closedir THISDIR; while (@allfiles) { $allfiles[0] =~ s/\s*\S+\.\S+\.*\S*\.*\S*\s*//; $array[$i] = $allfiles[0]; shift @allfiles; $i++ } shift @array; s/\s*//g; my $count = @array; $count++; while (@array2, $k<$count) { print "[$k] $array2[$j]\t"; $j++; $k++; }
I started off with some code from programming perl 2nd edition. The only problem is that I end up having 80 some array values, most of which are just spaces. I can't seem to get rid of them. Does anyone have any ideas? Would I be better off getting input from a dir system command? If so, how should I do this? Thanx.

- p u n k k i d
"Reality is merely an illusion, albeit a very persistant one."
-Albert Einstein

Replies are listed 'Best First'.
Re: Directory Listing
by plaid (Chaplain) on Jul 29, 2000 at 23:47 UTC
    Your code seems to be able to be fixed somewhat by changing the 'array2' instances in the second loop to just say 'array'. I don't see where array2 is supposed to be coming from.. It's not declared anywhere else in the code.

    It seems you're putting too much work into the code. If what you want is a listing of directories, you could do something as simple as

    use strict; opendir THISDIR, "." or die "serious dainbramagae: $!"; my $i = 0; my @files = readdir THISDIR; foreach (@files) { print "[", $i++, "] $_\t" if -d $_; } closedir THISDIR;
    Or, for a more clean, concise way
    use strict; opendir THISDIR, "." or die "serious dainbramagae: $!"; my $i = 0; print map { "[". $i++. "] $_\t" } grep { -d $_ } (readdir THISDIR); closedir THISDIR;
Re: Directory Listing
by athomason (Curate) on Jul 29, 2000 at 23:37 UTC
    I may not be understanding your question correctly (getting a list of directories), but what's wrong with this?
    #!/usr/bin/perl -w use strict; my $dirname = "."; opendir DIR, "$dirname" or die "couldn't open '$dirname': $!"; my @files = grep { -d $_ } readdir DIR; close DIR; print join "\n", @files;
    I'm especially uncertain of what you're trying to accomplish with all your regexps. Could you be more specific about your intent?

    Update:

    Cleaned up a bit for the strict -w police.

      Nothing's wrong with what you said, it worked great. I just am new to Perl, and don't know how to do everything yet. :) As to having the @array2 variable in my code, sorry about that. That was from one of my experiments to try to get it to work. My editor has this wacky Power Undo thing that undoes like 5 things at a time and I don't always catch it. Thanx for your help, I'm gonna try and understand it now.

      - p u n k k i d
      "Reality is merely an illusion, albeit a very persistent one."
      -Albert Einstein

Re: Directory Listing
by fundflow (Chaplain) on Aug 10, 2000 at 01:13 UTC
    Just to mention, this also lists the directories:
    for (<*.*>) { print $_ if -d; }
    It's better to use readdir() as mentioned above since it does not depend on the actual name (DOS has something with *.* and unix will not match directories without a dot). The advantage of this version is simplicity (and perlicity :)

      Actually, you don't want "start dot star" but simply "star". The latter will return only directories with a dot in it (in both Unix and DOS versions of perl)

      Even more perl-like:

      for (grep {-d} <*>) { ## Do stuff with $_; }

        FWIW I avoid idioms like the above ever since I got bitten by the fact that globbing is a shell operation. Very few will notice, but I have been in the position of needing to do an emergency rewrite when I passed the maxium number of files in a directory that could be listed. (What that limit is depends on the shell Perl was built with. That time it was a bit over 3000.) While readdir() is much more verbose, with it I have no fears about being bitten again.

        YMMV, warranty void where prohibited, etc.

        UPDATE
        jlp makes an excellent point below. I did not remember it, but once he said it I had a small bell go off. I had heard about that. Once 5.6.1 comes out I look forward to exploring what other goodness can be found therein.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-19 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found