APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Understanding Perl's map function

© September 2015 Anthony Lawrence

Anonymous asks:

I've seen "map" used in Perl code and I do not understand it at all. Can you explain it?

Perl's map function is often under-appreciated and not understood by Perl newbies. That's probably because it works on arrays, transforming one array into another array or hash. It's a time saver, both in typing time and when the code runs. Map does nothing you cannot do with a loop and if you are new to Perl a loop is likely exactly what you do use.

That may contribute to confusion. Because map turns up in code written by more experienced coders, that code tends to be more difficult for newbies to understand. That was certainly the case for me: I ignored map for years because I didn't understand it when I saw it. Even after I did understand it, I still tended to write loops for code I published here so as not to confuse newbies!

So what kinds of loops can map replace? Let's start with something simple: we have an array of numbers and want another array that contains those numbers times .75 when the number is greater than 49 and times .85 otherwise. Here's how you might code that in a loop:

my @array = (20, 10, 1, 100, 200, 50);
my @computed=();
foreach (@array) {
 $newvalue=$_ * .75;
 $newvalue=$_ * .85 if $_ < 50;
 push @computed,$newvalue;
print join(",", @computed), "\n";

The output of that would be "17,8.5,0.85,75,150,37.5". Now let's do it with map:

my @array = (20, 10, 1, 100, 200, 50);
my @computed= (map { $_ < 50 ? $_ * .85 : $_ * .75 } @array);
print join(",", @computed), "\n";

Honestly, that map line still gives me a headache. It's harder to read, but if your @array was large, that code would chew through it much more quickly.

Suppose instead we wanted to only extract the numbers greater than 49 and multiply those by .75? Our loop code might be:

my @array = (20, 10, 1, 100, 200, 50);
my @computed=();
foreach (@array) {
 next if $_ < 50;
 push @computed,$_ * .75;
print join(",", @computed), "\n";

The output of that would be "75,150,37.5".

Here's a map version:

my @array = (20, 10, 1, 100, 200, 50);
my @computed=(map { $_ > 49 ? $_ * .75 : ()} @array);
print join(",", @computed), "\n";

Again, faster but perhaps confusing at first glance.

Even more confusing is that you'll see map used like this:

my @array = (65,83, 81, 79, 100, 88, 75);
my @new_array = map (chr,@array);
print join(",", @new_array), "\n";

That produces "A,S,Q,O,d,X,K". Why parentheses instead of squiggly brackets? It's because initially we were writing code and code needs to be in a code block, but chr is another function. The map function can handle its arguments in two ways: map EXPR,LIST or map BLOCK LIST. Unfortunately for those of us with small brains, those squiggly brackets in Perl can also mean that you are about to reference a hash and not a code block. This leads us to headaches because map can be used to build hashes instead of arrays. See the first link below for more on that.

Perldoc.perl.org "map"

What's the point of Perl's map?

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Understanding Perl's map function

Inexpensive and informative Apple related e-books:

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of Apple Mail, Third Edition

iOS 10: A Take Control Crash Course

Take Control of High Sierra

Sierra: A Take Control Crash Course

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

I said that 30 years ago! Why does no one ever listen to me ? (Alan Kay)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode