Luba Pardo wrote:
Dear list,
Hello,
I want to parse through some files of a list of directories. Every
directory
have the same files, which means that I can make a loop and repeat the
process for each directory. I managed to write the code to process the
files
of a single directory but I do not exaclty how to read a list of
directories
and open one by one. I can only print the directories that are in the
parental directory , but I can't make the script to open the directory and
read the list of files. I do not what is the function to use to either open
everydir or read the 'subdirectories'. I hope someone can help
The beginning of the script looks like:
#! /usr/bin/perl
use strict;
use warnings;
my $pwd=$ENV{'PWD'};
use Cwd;
my $pwd = cwd;
my @filedir =<*ctt>;
You are populating @filedir with all entries ending in 'ctt', including those
that are *not* subdirectories.
foreach my $filedir (@filedir) {
opendir ($filedir) || die "can't \n";
perldoc -f opendir
You need a directory handle (same as open() needs a filehandle.) You should
also include the $! variable in the error message so you know *why* it failed.
my @f = <mlc*>;
You are populating @f with all entries beginning in 'mlc' in the *current*
*directory*, not in the $filedir directory.
foreach my $f(@f) {
open (IN,"$f") or die "$!\n";
perldoc -q quoting
You are trying to open the file $f in the *current* *directory*, not in the
$filedir directory.
print "$f\n";
my @a2= (<IN>);
...
You *may* want someting like this (UNTESTED):
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
my $pwd = cwd;
for my $filedir ( grep -d, <*ctt> ) {
for my $f ( grep -f, <$filedir/mlc*> ) {
open my $IN, '<', $f or die "Cannot open '$f' $!\n";
print "$f\n";
my @a2 = <$IN>;
...
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/