in reply to Check if at least one element of array is bigger than X

The conventional Perl tool for finding stuff in arrays is grep. Consider:

if (grep {$_ > 10} @array) { print "Yes, at least one is bigger than 10\n"; }

In this case grep returns a list containing all the elements that match the > 10 test. In a scalar context (which is the case for an if expression) the number of elements in the list is given so the test is really "are there any elements who's value is > 10?". grep implies the loop btw, but wraps it up in a pretty package.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Check if at least one element of array is bigger than X
by Anonymous Monk on Apr 08, 2010 at 01:23 UTC
    <O>so the test is really "are there any elements who's value is > 10?"

    Maybe logically, but it still checks the entire list.

      but it still checks the entire list

      So what? Maybe that is an issue if the list is tens of thousands of items long and the test is being performed often, but in most cases neither criteria is met and the simplistic grep solution is by far the simplest and most maintainable technique. For the vast majority of cases avoiding grep for efficiency reasons is a perfect example of why we implore people to avoid premature optimisation.

      When I was writing my original reply I considered opening that particular can of worms, but given the nature of the OP's question decided that the additional layer of confusion wasn't warranted - sigh.

      True laziness is hard work
        So what?

        Its a caveat you omitted.