etherald has asked for the wisdom of the Perl Monks concerning the following question:

How can I write this to get an odd value >=3? It lets 1 through.
($lines && $lines %2) or ($lines > 3 or $lines=3);
composure/composure

Replies are listed 'Best First'.
Re: getting my value as i like it to
by roboticus (Chancellor) on Feb 10, 2015 at 01:14 UTC

    etherald:

    If you break your condition down into chunks you should be able to see the problem. You can do it something like this:

    #!/usr/bin/perl use strict; use warnings; for my $lines (0 .. 4) { print "lines=$lines: "; print $lines ? "A " : " "; print $lines%2 ? "B " : " "; print $lines && $lines%2 ? "C " : " "; print $lines>3 ? "D " : " "; print $lines==3 ? "E " : " "; print( ($lines>3 or $lines==3) ? "F " : " "); print( (($lines && $lines%2) or ($lines>3 or $lines==3)) ? "G " : +" "); print "\n"; }

    When you run it, it'll print a truth table of the various bits of your expression. If your whole expression is true, you'll get a G on the row. (Note: "C" is the result of the bit in your first set of parenthesis, and "F" is for the bit in the right pair of parenthesis.) Running it give you:

    $ perl t.pl lines=0: lines=1: A B C G lines=2: A lines=3: A B C E F G lines=4: A D F

    So when $lines==1, you get a G because the first condition is satisfied. If you change the or between your parenthesis to and, you'll get what you want.

    Even so, your expression is too complicated for what you want. You really want to keep it if it's an odd number greater than or equal to three, right? If so you want:

    $lines%2 and $lines>=3

    Note: $lines=3 is an assignment rather than a conditional, so I changed it to $lines==3.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks, I knew it would be simpler. That's what you get when you write perl after a day of doing Java :\ I was trying to do the assign as the final condition.
      composure/composure