pairfirst#

Return the first pair for which a block returns true.

תקציר#

my ($k, $v) = pairfirst { BLOCK } @kvlist;
my $found   = pairfirst { BLOCK } @kvlist;   # scalar context

For each pair the block is called in scalar context with $a set to the key and $b set to the value. pairfirst stops at the first pair for which the block returns true.

מה מוחזר#

In list context, the matching ($key, $value) pair, or an empty list if nothing matched. In scalar context, a boolean indicating whether any pair matched. $a and $b are aliased to the original list elements.

דוגמאות#

my ($k, $v) = pairfirst { $a =~ /^upper_/ } %config;
if ( pairfirst { $b > 1_000 } %totals ) {
    warn "at least one big entry";
}

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

Fully compatible with upstream.

ראו גם#

  • first - non-pair equivalent, uses $_.

  • pairgrep - collect every matching pair.