pairmap#

Run a block for each pair and concatenate the results.

תקציר#

my @out   = pairmap { BLOCK } @kvlist;
my $count = pairmap { BLOCK } @kvlist;   # scalar context

For every pair the block is called in list context with $a set to the key and $b set to the value; its results are appended to the output list in order.

מה מוחזר#

In list context, the concatenation of everything every invocation of the block returned. In scalar context, the number of items that would have been returned in list context. $a and $b are aliased to the original list elements - modifications in the block are visible to the caller.

דוגמאות#

my @lines = pairmap { "The key $a has value $b" } %hash;
my @flat  = pairmap { ( $a, uc $b ) } %h;   # values upper-cased
my @pairs = pairmap { [ $a, $b ] } %h;      # equivalent to `pairs`

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

Fully compatible with upstream.

ראו גם#

  • pairgrep - filter pairs by a predicate.

  • pairfirst - stop at the first matching pair.

  • pairs - bundle pairs into blessed arrayrefs.