pairgrep#

Keep the pairs for which a block returns true.

Synopsis#

my @kept  = pairgrep { BLOCK } @kvlist;
my $count = pairgrep { BLOCK } @kvlist;   # scalar context

For every pair the block is called in scalar context with $a set to the key and $b set to the value. Pairs for which the block returns true are included in the result; others are dropped.

What you get back#

In list context, an even-sized list: the kept keys and values interleaved. In scalar context, the number of pairs (not elements) that matched — half of what the list-context length would be. $a and $b are aliased to the original list elements.

Examples#

my %big     = pairgrep { $b > 100 } %totals;
my @uppers  = pairgrep { $a =~ /^[A-Z]/ } %h;
my $n_big   = pairgrep { $b > 100 } %totals;   # scalar: pair count

Differences from upstream#

Fully compatible with upstream.

See also#

  • pairmap — transform pairs instead of filtering.

  • pairfirst — find the first matching pair and stop.