Prev: sort values

Transform arrays and lists using map

The map function allows us to create a compact expression transforming each element of an array or that of a list to some other kind of value. Perl 5 used the same map keyword. In Python this is usually accomplished with list comprehension.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.
Specifially there is a related article: Transforming a Perl array using map.

map as a function

In the first example we created an array of 3 numbers and then used the map function to create a new array that will contain the duplicate of each original value. The variable $_ contains the current element of the array. Effectively, map is a loop that iterates over the array on the right hand side, assigns each element to $_ variable, executes the code in the block, and collects the results. The collected list is assigned to the array on the left hand side of the = assignment.

In the second example, instead of using $_, the default variable name, we another variable type that is auto-generated. We could have used any variable name there. The caret ^ after the $-sign told perl that this is an autogenerated variable. This has exactly the same job as $_ but if you select a good name will make your code much more readable.

The perl method helps us to dump the content of the array.

Finally we printed the original array showing that it has not changed.


use v6;

my @numbers = 2, 5, 7;
say @numbers.perl;    #, 5, 7)

my @d = map { $_ * 2 }, @numbers;
say @d.perl;          #, 10, 14)

my @x = map { $^number * 2 }, @numbers;
say @x.perl;          #, 10, 14)

map as a method

The same functionality is available as a method as well.

In this example we created the same array and then called the map method on it. In the first call we used the arrow operator to create an internal variable. Much like it is done in regular for-loops. Then, just as in a for-loop a block follows with the actual expression.

In the second expression the first * is the Whatever operator, which is anothe placeholder of the "current value". I think this version is not very readable so I'd not recommend it.


use v6;

my @numbers = 2, 5, 7;
say @numbers.perl;    #, 5, 7)

my @d = -> $x {$x * 2 } );
say @d.perl;      #, 10, 14)

my @b = * * 2 );
say @b.perl;     #, 10, 14)

say @numbers.perl;    #, 5, 7)

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2014-12-31

Prev: sort values


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.3595185

Support my Perl 6 book!

I am writing a book on Web Application Development in Perl 6 and raising money via crowdfunding. Please support my effort!

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.