Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

If statements with . and .. filehandles.

by sparkyichi (Deacon)
on Jun 18, 2002 at 20:14 UTC ( #175487=perlquestion: print w/ replies, xml ) Need Help??
sparkyichi has asked for the wisdom of the Perl Monks concerning the following question:

I can't figure out why my directory listing with an if statement is not working the way I would expect. I want to exclude . and .. from showing up in the list so I did the following:
my $dirname = '.'; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { if ($file ne '.' || $file ne '..'){print "$file\n";} } closedir(DIR);
This prints out a complete list including . and .. I then tried the following to produce only a . and .. listing:
my $dirname = '.'; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { if ($file eq '.' || $file eq '..'){print "$file\n";} } closedir(DIR);
And it works only printing the following:
.
..

Why does it work one way and not the other?

B.T.W. I know that this works great:
opendir (DIR, ".") or die "$!"; @dir = grep !/^\.\.?$/, readdir DIR; closedir DIR;


Sparky
FMTEYEWTK

Comment on If statements with . and .. filehandles.
Select or Download Code
Re: If statements with . and .. filehandles.
by FoxtrotUniform (Prior) on Jun 18, 2002 at 20:22 UTC
      if ($file ne '.' || $file ne '..'){print "$file\n";}

    This is always true. You probably want:

    if ($file ne '.' and $file ne '..'){print "$file\n";}

    --
    The hell with paco, vote for Erudil!
    :wq

Re: If statements with . and .. filehandles.
by thelenm (Vicar) on Jun 18, 2002 at 20:22 UTC
    You should change your || to &&. With the || test, a filename will pass one condition or the other regardless of the filename is.

    -- Mike

    --
    just,my${.02}

Re: If statements with . and .. filehandles.
by tadman (Prior) on Jun 18, 2002 at 20:22 UTC
    It would seem you've botched your logic. The correct assertion should be:
    if ($file ne '.' && $file ne '..')
    You will not that when you inverted the comparator, you didn't invert the logical operator, so you didn't get the inverse set.

    This could also be written using inverted operators:
    unless ($file eq '.' || $file eq '..')
    If you think about why it didn't work, consider this statement:
    "If the sky isn't blue, or the sky isn't black, then it must be overcast."
    Now, you might think that the 'or' condition is cumulative, as if one condition builds on the other, but they're independent. If the sky is blue, then it isn't black. If it's black, then it isn't blue. At least one part of this statement must be true, so the assertion holds in all cases, regardless of the colour of the sky.

    On the other hand, if you said:
    "If the sky isn't blue, and the sky isn't black, then it must be overcast."
    In this case it is cumulative, where both conditions must be true in order for the assertion to be made.
Re: If statements with . and .. filehandles.
by tune (Curate) on Jun 18, 2002 at 21:09 UTC
    Or use the mighty regexp solution.
    print "$file\n" if $file !~ /^\.{1,2}$/;

    --
    tune

      Correction:
      print "$file\n" if $file !~ /\A\.{1,2}\Z/;
      Using ^ and $ would also match files named .\n or ..\n.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://175487]
Approved by particle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-11-28 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls