Understanding Perl's map function

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:


#!/usr/bin/perl
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:

#!/usr/bin/perl
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:

#!/usr/bin/perl
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:

#!/usr/bin/perl
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:

#!/usr/bin/perl
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




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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





The teaching of BASIC should be rated as a criminal offence: it mutilates the mind beyond recovery. (Edsger W. Dijkstra)

FORTRAN—the "infantile disorder"—, by now nearly 20 years old, is hopelessly inadequate for whatever computer application you have in mind today: it is now too clumsy, too risky, and too expensive to use. (Edsger W. Dijkstra)








This post tagged: