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

Ok, I have a bunch of files. All of these files have the same name. All of these files with the same name are in a directory with the same name. Each of these directories with the same name is a subdirectory in a bunch of directories all with different names. All of these directories are subdirectories of one common directory. So the list of files would look like this:

./dir1/foo/bar.txt

./dir2/foo/bar.txt

./dir3/foo/bar.txt

etc.

I need to make the same modification to every copy of bar.txt. My plan was to find a way to make an array dirList such that its contents were equal to ( dir1 dir2 dir3 ... ). Therefore I could just say

foreach my $dir ( @dirList ) { open ( FILE, "$dir/foo/bar.txt" ) or die ("nasty message: $!"); while (<FILE>) { &doStuff(); } }
My only problem is I don't know how to create @dirList. Any help would be appreciated.

Some people drink from the fountain of knowledge, others just gargle.

Replies are listed 'Best First'.
Re: Opening every directory in the current directory
by japhy (Canon) on Feb 10, 2001 at 02:39 UTC
    You want to get a list of the entries in a given directory that are also directories. This is done with readdir() and the -d test:
    opendir DIR, $some_directory or die "can't read $some_directory: $!"; @dirList = grep -d "$some_directory/$_", readdir DIR; closedir DIR;
    Please read the appropriate documentation (perlfunc).

    japhy -- Perl and Regex Hacker
      You may as well get a list of the just the directories with the desired file, or else the open() in the original post's code will fail:
      @DirList = grep { -f "$some_directory/$_/foo/bar.txt" } readdir DIR;
Re: Opening every directory in the current directory
by runrig (Abbot) on Feb 10, 2001 at 02:42 UTC
    You could build an array with File::Find, but a simpler way would be to use file globbing to just get the file names directly:
    while (my $file=<*/foo/bar.txt>) { #Open/read $file and do Stuff }
Re: Opening every directory in the current directory
by footpad (Abbot) on Feb 10, 2001 at 08:28 UTC