AnnaSchumann AnnaSchumann - 7 months ago 9
Perl Question

IF statement containing OR perl

A very simple question but I can't seem to figure out the problem. Why does this snippet of code successfully print all lines where $F[1] does not equal 83:

if ($F[1] != 83) {
print OUT2 "$_\n";
}


But this snippet (containing an OR statement) simply prints all lines, ignoring both criteria:

if ($F[1] != 83 || $F[1] != 99) {
print OUT2 "$_\n";
}


The desired result was to print all lines where $F[1] contains anything other than 83 or 99.

Answer

Since every number is either != 83 or != 99 or both, using || won't work.

Use && instead to get only numbers other than 83 and 99:

if ( $F[1] != 83 && $F[1] != 99 ) {

If you have a longer list to check, you may want to do e.g.:

if ( ! grep $_ == $F[1], 83, 99, 107, 133, 150 ) {