first#

Return the first list element for which a block returns true.

תקציר#

my $val = first { BLOCK } @list;
my $d   = first { defined } @list;
my $big = first { $_ > 100 } @values;

For each element first sets $_ to that element and calls the block in scalar context; the first element for which the block returns true is returned. Remaining elements are not examined.

מה מוחזר#

The matching element (the original value, not the block’s return). If no element matches - or the list is empty - returns undef.

דוגמאות#

my $first_defined = first { defined $_ } @list;
my $first_big     = first { $_ > $threshold } @measurements;
my $first_admin   = first { $_->is_admin } @users;

הבדלים מן ה-upstream#

Fully compatible with upstream.

ראו גם#

  • any - boolean ”at least one matches“ test.

  • grep - collect every matching element.

  • pairfirst - pair-oriented counterpart.