0

Here is my line of code:

#!/usr loca/bin/perl                                                           
use warnings;                                                                   
use strict;  
...
...
if ($file ne "." || $file ne "..")

I have also tried:

if ($file ne "." or $file ne "..")

And the if statement always passes since it cannot recognize the conditions I am setting. I have done one at a time and they work as expected. Can anyone help determine how I need to set this up properly? Thanks!

lchristina26
  • 205
  • 5
  • 20
  • 2
    No, it always passes because both of your conditions cannot be false simultaneously. Try using `&&/and` instead of `||/or` and see if your are more satisfied with the results. – mob Sep 11 '15 at 17:42
  • 1
    This looks like an AND condition: `&&`. – Matt Jacob Sep 11 '15 at 17:42
  • 1
    Here's a list of things that are not equal to `.`: `foo`, `1`, `..`. Here's a list of things that are not equal to `..`: `bar`, `2`, `.`. Here's a list of things that are in either the first list *or* the second list: `foo`, `bar`, `1`, `2`, `.`, `..`. In other words, everything! – ThisSuitIsBlackNot Sep 11 '15 at 18:01

1 Answers1

4

Everything is either not a single dot or not a double dot!

You mean

if ( $file ne "." and $file ne ".." ) { ... }

and you should probably be looking at glob rather than opendir / readdir

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 2
    Globbing is not necessarily better, especially for [portability](http://perldoc.perl.org/perlport.html): "Don't count on filename globbing. Use `opendir`, `readdir`, and `closedir` instead." – Matt Jacob Sep 11 '15 at 17:51
  • 4
    recommend using `&&`, not `and`, and reserving `and`for flow control – ysth Sep 11 '15 at 18:51
  • Thanks, simple logic oversight on my end that was probably not worthy of a StackOverflow question. – lchristina26 Sep 11 '15 at 19:10
  • 1
    @lchristina26 It's actually a very common mistake. See [this question](http://stackoverflow.com/q/29944441/176646), for example (possible duplicate?). Wrapping your brain around multiple negatives at once is tough; I prefer statements like `next if $file eq '.' || $file eq '..';` since they're easier to understand at a glance. – ThisSuitIsBlackNot Sep 11 '15 at 20:03