1. Home
  2. Computing & Technology
  3. Perl

Perl array map() function - Quick Tutorial

How to use the array map() function

By Kirk Brown, About.com

@LIST = map(EXPRESSION, @ARRAY);
Perl's map() function runs an expression on each element of an array, and returns a new array with the results.
@myNames = ('jacob', 'alexander', 'ethan', 'andrew');
@ucNames = map(ucfirst, @myNames);
Think of the @myNames array as a row of numbered boxes, going from left to right, numbered starting with a zero. The map() function goes through each of the elements (boxes) in the array, and runs the EXPRESSION on their contents. The altered contents are then added to the new @ucNames array.

In the above example, EXPRESSION is the built-in function ucfirst that makes the first letter of the word upper case. After running the expression on each element of the @myNames array, the value of @ucNames becomes ('Jacob', 'Alexander', 'Ethan', 'Andrew').

More Perl Quick Tips

Explore Perl

More from About.com

  1. Home
  2. Computing & Technology
  3. Perl
  4. Programming Perl
  5. Perl map function reference - learn how to use Perl's map() function in this quick tutorial.

©2008 About.com, a part of The New York Times Company.

All rights reserved.