Alan_C wrote:
> [ . . ]
>
> Hi,
>
> my @new = grep /[^.]/, readdir DIR;
>
> (on Linux I tried it) that eliminates . and .. from the catch.
>
> *But the next code prints all 8 lines of data it does not eliminate . and ..
> from the catch, print*
>
> Isn't that a character class that says "not a dot"
>
> So, why the difference (readdir line above versus the next code)?  Is it a
> difference of list context (the grep) versus scalar context (ea. line of DATA
> is a string) ???
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> while ( <DATA> ) {
> print if /[^.]/;
> #print "$_\n" if grep /[^.]/, $_;
> #print unless /\./;
> #print if /star/;
> }
> __DATA__
> star.txt
> things
> .dotfile
> aft
> .
> ..
> filename.htm
> stuff

Hi Alan

Your code doesn't filter out any of the lines because they still contain the
line terminator "\n" from the read, which will match /[^.]/. Changing the loop
to:

  while ( <DATA> ) {
    chomp;
    print "$_\n" if /[^.]/;
  }

will give the results you expect. The chomp was unnecessary in the previous case
becuase readdir() returns the actual filenames without any line terminator.

By the way, Randal has pointed out in a private mail that my construct is
unworkable because it filters out the possibly valid filename '...' as well as
the current and parent directory names '.' and '..'. I will leave it to you to
decide whether you think this is a risk worth taking.

HTH,

Rob




--
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