June 2002
Beginner
759 pages
80h 42m
English
reduce
reduce { BLOCK } @list
Literally “reduces” @
list by calling
BLOCK until there are no more items to
operate on in @
list. reduce sets $a and $b for each operation in
BLOCK and returns the reduced list as a
scalar. If @
list is 0, BLOCK is
not executed, and $
list [0] is returned. If
@list is empty, then reduce returns undef.
You can sum and concatenate a list using reduce like so:
my $sum_of_list = reduce { $a + $b } @ll; # sum
my $concat_list = reduce { $a . $b } @ll; # concat