← All languages
Perl construct of the day
Random

map

Apply a transformation to each element of a list.

Description

The map function evaluates a block or expression for each element of a list and returns a list of the results. Inside the block, each element is aliased to $_.

The block can return any number of values for each input element, including zero values (filtering) or multiple values (expanding). This makes map more flexible than a simple one-to-one transformation.

map is one of Perl's core functional programming tools. Combined with grep and sort, it enables powerful data-processing pipelines without explicit loops.

Arguments

NameDescriptionOptional
BLOCK An expression or block evaluated for each element. The element is available as $_. No
LIST The list of values to transform. No

Example

my @nums = (1, 2, 3, 4);
my @doubled = map { $_ * 2 } @nums;
# @doubled is (2, 4, 6, 8)

my @upper = map { uc } @words;

Reference