From f94762723eddc4a0de865db28c07e020aecf8d06 Mon Sep 17 00:00:00 2001 From: Alexander Hartmaier Date: Fri, 11 Feb 2011 15:36:21 +0100 Subject: [PATCH] perldoc improvements for map --- pod/perlfunc.pod | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index ece1005..d85b3d7 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -2996,9 +2996,26 @@ total number of elements so generated. Evaluates BLOCK or EXPR in list context, so each element of LIST may produce zero, one, or more elements in the returned value. - @chars = map(chr, @nums); + @chars = map(chr, @numbers); -translates a list of numbers to the corresponding characters. And +translates a list of numbers to the corresponding characters. + + my @squares = map { $_ * $_ } @numbers; + +translates a list of numbers to their squared values. + + my @squares = map { $_ > 5 ? ($_ * $_) : () } @numbers; + +shows that number of returned elements can differ from the number of +input elements. To omit an element, return an empty list (). +This could also be achieved by writing + + my @squares = map { $_ * $_ } grep { $_ > 5 } @numbers; + +which makes the intention more clear. + +Map always returns a list which can be assigned to a hash where the elements +become key/value pairs. See L for more details. %hash = map { get_a_key_for($_) => $_ } @array; -- 2.7.4