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.
|
|---|
| 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 | |
by GrandFather (Saint) on Apr 08, 2010 at 01:36 UTC | |
by Anonymous Monk on Apr 08, 2010 at 01:41 UTC | |
by ikegami (Patriarch) on Apr 08, 2010 at 02:01 UTC |