Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Directory Listing to array

by Poetic Justice (Monk)
on Apr 09, 2002 at 21:18 UTC ( [id://157858]=perlquestion: print w/replies, xml ) Need Help??

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

I'm using readdir to extract the listings of the files in a directory. I would like to take the values returned and push them onto an array. Currently the code looks like--
while ( $standir = readdir( DIR ) ) { $type = ( -d "$path\\$standir" ) ? "dir" : "file"; if ($standir =~ m/^\./) { $standir = ''; } else { $standir = "$standir" ."\n"; push(@standir,$standir); } }

When I test the code (attempt to pop and print the contents of the array), there is nothing there. What am I missing?
Many thanks to the community.
Poetic Justice

Replies are listed 'Best First'.
Re: Directory Listing to array
by Zaxo (Archbishop) on Apr 09, 2002 at 21:27 UTC

    See readdir, it returns a list.

    my @standir = grep {! /^\./} readdir(DIR);
    But perhaps glob is more what you want. It will ignore dotfiles.

    After Compline,
    Zaxo

      Actually while (my $file = readdir DIR) { print "$file\n" } will happily iterate over readdir. Try it. Also your grep solution will skip files like:

      . .. .bashrc .any_other_unix_style_hidden_file/dir

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Directory Listing to array
by Zed_Lopez (Chaplain) on Apr 09, 2002 at 21:43 UTC
    Did you open DIR with an opendir and not just an open? The department of brevity department suggests:
    !/^\./ and push @standir, "$_\n" while ($_ = readdir(DIR)) # or @standir = grep { !/^\./ } readdir(DIR); # doesn't append \n
Re: Directory Listing to array
by tachyon (Chancellor) on Apr 09, 2002 at 23:27 UTC
      Definitely a good idea, and the sample code is helpful. When I finally finish the project I'll post the full source.
      Thanks monks, ,
      Poetic Justice
Re: Directory Listing to array
by Poetic Justice (Monk) on Apr 09, 2002 at 22:48 UTC
    Getting readdir to return what I want is not the problem. The problem is the array. I've been testing and I finally got the array to be populated using split(/\s/, $standdir) on the list returned by readdir. I've got get the proper control structure in place to pop the data off the array. while(newfil = pop @standir) isn't working for me.
    As for brevity, I have to write this script to be used by non-perlmonks. They are VB heretics.
    Poetic Justice
Re: Directory Listing to array
by Poetic Justice (Monk) on Apr 10, 2002 at 19:37 UTC
    As promised here is the finished code. Mad props and thanks to tachyon, and Zaxo for their comments and assistance.
    I would post the whole progam, but all it does is convert the standard IIS Log file to fixed width format for upload to our datawarehouse. Our datastaging component wants files in this format. Don't ask me why, that was before my time at my current employer.
    sub listfiles { $path = shift; $path = "." unless $path; opendir( DIR, $path ) || die "Can't open $path: $!"; !/^\./ and push @loot, "$_\n" while ($_ = readdir(DIR)); closedir( DIR ); return @loot; }

Log In?
Username:
Password:

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

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

    No recent polls found