Name

map

Synopsis

map {block} list map expr, list

Description

Evaluates the block or expr for each element of list (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation. It evaluates block or expr in a list context, so each element of list may produce zero, one, or more elements in the returned value. These are all flattened into one list. For instance:

@words = map { split ' ' } @lines;

splits a list of lines into a list of words. Often, though, there is a one-to-one mapping between input values and output values:

@chars = map chr, @nums;

This statement translates a list of numbers to the corresponding characters.

Get Perl in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.