in reply to How to Do Multiple OR in For Loops

Your "if" statement is very efficient. There is no need to change it unless you need to make a major expansion of the $predX variables, probably with a @pred array. Otherwise, I see no need to re-visit and change working code.

Do not mistake fewer lines of code for greater efficiency.
However, ikegami's code looks to me to be short, efficient and uses the power of the language, ie. grep.

Replies are listed 'Best First'.
Re^2: How to Do Multiple OR in For Loops
by JavaFan (Canon) on Apr 13, 2011 at 10:42 UTC
    Note though that || short circuits (doesn't evaluate more clauses than needed to calculate the answer), where as grep will always perform all checks.

    Of course, it's not very common to have so many clauses that it matters. You may need hundreds, if not thousands of clauses to make a significant difference (and then only if there's a true clause "early" enough).

      I was wondering if the short circuit could cause side effects. But in this case there is no risk of auto-vivication.

      With side-effects I'd rather prefer a non short circuit solution for clarity.

      If a short circuit effect is side effects areš wanted, it should be done explicitly.

      Cheers Rolf

      1) corrected bad phrasing!

        If a short circuit effect is wanted, it should be done explicitly.
        That appears to be saying "if you want the short circuit effects of || and &&, you shouldn't use || and &&, but do it explicitly, but it's ok to use || and && (and get the short circuiting) if you don't want it".

        But that would be strange, and it's probably not what you wanted to communicate.

        Personally, I very much prefer to write things like:

        if (ref $code && $code->()) {...} # short circuit
        instead of
        if (ref $code) { if ($code->()) { # explicitly ... } }
        It's a long time since I coded Pascal, and I don't miss much of it. Actually, I don't think I miss anything from it.