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

dear monks, i want to list the directories after checking if its available or not. example, "/home/folders/" has directories namely 'case1','case2','case3','case4', so on..... under each case, there are many directories, but i want to check for the availabilities of only 3 directories namely "SORTED", "CLEANED" and "PROCEED". The way i want to print the results is: (assuming that the "no"s are the directories which are absent in few of the cases)....
CASENAME SORTED CLEANED PROCEED case1 yes no no case2 yes yes yes case3 no yes yes case4 no no no
What i have done so far is: (but am not getting the desired results)
#!/usr/bin/perl open(FILE,'>out.txt') or die $!; if($one eq "option1"){ opendir(DIR,"/home/folders/") or die "can not open directory"; foreach my $line (sort grep !/^\./, readdir DIR){ print FILE "$line|"; opendir(DIR1,"/home/folders/$line"); foreach my $line1 (sort grep !/^\./, readdir DIR1){ if($line1=~/sorted/ && $line1=~/^\w/){print FILE "Yes|";} elsif($line1=~/cleaned/ && $line1=~/^\w/){print FILE "Yes|";} elsif($line1=~/proceed/ && $line1=~/^\w/){print FILE "Yes|";} } print FILE "\n"; } } close (FILE); open(FILE1,'out.txt') or die $!; print "CASENAME SORTED CLEANED PROCEED"; while($ln=<FILE1>){ ($directory,$subdir1,$subdir2,$subdir3)=split(/\|/,$ln); print "\n$directory\t"; if($subdir1 ne "Yes"){print "No\t";}else{print "Yes\t";} if($subdir2 ne "Yes"){print "No\t";}else{print "Yes\t";} if($subdir3 ne "Yes"){print "No\t";}else{print "Yes\t";} } close(FILE1); unlink("out.txt");
Thank u :)

Replies are listed 'Best First'.
Re: printing the directories...
by moritz (Cardinal) on Oct 23, 2008 at 07:29 UTC
    I think forms could help you. Or Text::Table.

    And I think you're making it more difficult than it is:

    use Text::Table; my $t = Text::Table->new(qw(CASENAME SORTED CLEANED PROCEED)); for my $dir (glob '/home/folders/case*/') { my @res; for my $subdir (qw(sorted cleaned proceed) { if (-d "$dir/$subdir") { push @res, 'Yes'; } else { push @res, 'No'; } } $t->add(@res); } print $t;

    Untested, but should work that way (or with small modifications).

    See -d for documentation of -d.

        Don't be mislead by that name. Although Perl6::Form was a prototype for something that was planned for Perl 6, Forms/formats aren't part of the language specification anymore (and to be handled by libraries instead).

        I've never been into formats, but have used Text::Table before, and I find its interface intuitive, and much more quickly to grasp than formats/forms magic.

      thanks...the program gives me the desired results..though, i m not able to print the case names. pushing, $dir to @res doesnt help because its printing the full path and not the case folder name alone :(
Re: printing the directories...
by andreas1234567 (Vicar) on Oct 23, 2008 at 11:39 UTC
    Like moritz I would use Text::Table: Which would produce
    $ perl -w 718972.pl +---------+---------+--------+---------+ | | CLEANED | SORTED | PROCEED | +---------+---------+--------+---------+ | ./case1 | x | x | | | ./case2 | x | x | | | ./case3 | x | x | | | ./case4 | x | x | | | ./case5 | x | x | | | ./case6 | x | | | | ./case7 | x | | | | ./case8 | x | | x | | ./case9 | x | | x | +---------+---------+--------+---------+
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: printing the directories...
by jwkrahn (Abbot) on Oct 23, 2008 at 10:49 UTC

    This seems to do what you require:

    #!/usr/bin/perl use warnings; use strict; use File::Basename; print "CASENAME SORTED CLEANED PROCEED\n"; my @fields = qw/ sorted cleaned proceed /; for my $path ( grep -d, </home/folders/case*> ) { my $case = basename $path; my %subdir = ( $case => { map { $_ => 'No' } @fields } ); for ( map basename( $_ ), grep -d, <$path/*> ) { /^\w/ && /sorted|cleaned|preceed/ and $subdir{ $case }{ $_ } = + 'Yes'; } print join( "\t", $case, @{ $subdir{ $case } }{ @fields } ), "\n"; }