Copyright © tutorialspoint.com

PERL map Function


Syntax

map EXPR, LIST

map BLOCK LIST


Definition and Usage

Evaluates EXPR or BLOCK for each element of LIST. For each iteration, $_ holds the value of the current element, which can also be assigned to allow the value of the element to be updated.

Simply, Perl's map() function runs an expression on each element of an array, and returns a new array with the results.

Return Value

  • In scalar context, returns the total number of elements so generated.

  • In list context, returns list of values

Example

Try out following example:

#!/usr/bin/perl -w

@myNames = ('jacob', 'alexander', 'ethan', 'andrew');
@ucNames = map(ucfirst, @myNames);

foreach $key ( @ucNames ){
 print "$key\n";
}

It will produce following results:

Jacob
Alexander
Ethan
Andrew

Copyright © tutorialspoint.com