This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at
http://faq.perl.org .
--------------------------------------------------------------------
4.45: How do I find the first array element for which a condition is true?
To find the first array element which satisfies a condition, you can use
the "first()" function in the "List::Util" module, which comes with Perl
5.8. This example finds the first element that contains "Perl".
use List::Util qw(first);
my $element = first { /Perl/ } @array;
If you cannot use "List::Util", you can make your own loop to do the
same thing. Once you find the element, you stop the loop with last.
my $found;
foreach ( @array ) {
if( /Perl/ ) { $found = $_; last }
}