On Wednesday 26 July 2006 00:59, Rob Dixon wrote:
> Hello Alan
>
> Alan_C wrote:
>  > On Tuesday 25 July 2006 15:56, Rob Dixon wrote:
>  >>Nishi Bhonsle wrote:
[ snip ]
>  > [ snip ]
[ snip ]
>  > #!/usr/bin/perl
>  >    use strict;
>  >    use warnings;
>  >
>  >    my @dir = do {
>  > #     opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
>  >      opendir my $dh, '/home/al/temp4' or die $!;
>  > #     grep -f, readdir $dh;
>  >      readdir $dh;
>  >    };
>  >
>  >    print "$_\n" foreach @dir;
[ snip ]
>  > That works on Linux (not matters the current dir, I was in /home/al when
>  > I ran it) which leads me to guess that something be wrong with the grep
>  > line.
[ snip ]
> but the error in my code was that without a full path
> the -f operator will look for the file in the current directory to decide
> whether it is a plain file or not instead of in its proper place. Files
> were being filtered out of the list because they didn't exist at all (in
> the current directory), not because they were the unwanted directories.
> Your code succeeds because you have removed the grep() but now it no longer

Ok, *now* I get it.

my @dir = do {
#     opendir my $dh, 'C:\build\Sample\NewDir' or die $!;
      opendir my $dh, '/home/al/temp4' or die $!;
#     grep -f, readdir $dh;

Therein, the grep works on the $dh (/home/al/temp4) while the -f operator 
works on the current dir [not (sp) necisarily /home/al/temp4]

Thus the fixed version has the full path in the grep line (or, obviously, 
could first change dir to desired dir before doing the grep -f)

I tinkered with it a bit, added a file filter capability.

#!/usr/bin/perl
   use strict;
   use warnings;

   my $path = '/home/al/temp4'; # c:/multi/platform

   my @dir = do {
     opendir my $dh, $path or die $!;
     grep -f "$path/$_", readdir $dh;
   };

#   print "$_\n" foreach @dir; # all files

   foreach ( @dir ) {
   print $_, "\n" if /\.htm/i; # my file filter
 }# end

-- 
Alan.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to