Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

map

  • map

map will transform the content of a list.

Given a list of values that can come from an array or from calling the keys function on a hash or in any other way, we can apply a transformation on each element and then collect the transformed values on the left hand side of the assignment. e.g. in an array.

On each iteration the current element is placed in the $_ variable, the code in the block is executed, and the result is passed to the left-hand-side that collects all the responses.

ARRAY = map BLOCK LIST
#!/usr/bin/perl
use strict;
use warnings;

my @numbers = (1, 4, 7, 3);
my @doubles = map {$_ * 2} @numbers;
print "@doubles\n";  # 2 8 14 6