in reply to Nested testing of many conditions--a better way?

Update: Added parens around 'or' condition.

ambrus++ is correct. I should know better than to post without testing.

It's very much a personal thing, but I'd opt for the latter, but I'd format it like this:

if ( $today > $start and $today < $stop and $today != $notthisday[0] and $today != $notthisday[1] and $today != $notthisday[2] and ( $day == $dayofmon[0] or $day == $dayofmon[1] ) ) { print "$today is the day."; }

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: Nested testing of many conditions--a better way?
by halley (Prior) on Mar 23, 2004 at 14:52 UTC
    Agreed. The cascade of tests with and is preferable to my eye.

    Just a note that there is a difference between and vs &&. The difference is order of precedence. BrowserUK used the word form so that he didn't have to worry about order of precedence so much. You can also use parentheses with either form to ensure the precedence is correct. The original example is in danger of accidentally mixing up precedence, and getting a completely wrong result.

    A third alternative, in some cases, is the "quick fail." This is a series of reasons to break out of a loop or return from a subroutine early.

    while (<>) { next if /^\s*\#/; # comment next if /^\s*$/; # empty line do_the_work($_); }
    is equivalent to
    while (<>) { if (not /^\s*\#/ and not /^\s*$/) { do_the_work($_); } }

    --
    [ e d @ h a l l e y . c c ]

Re: Re: Nested testing of many conditions--a better way?
by ambrus (Abbot) on Mar 23, 2004 at 15:19 UTC

    I think this code is wrong, as the or operator has a lower precedence than and, so you should either change it to || or parenthisize the last two lines of the condition.