perl function map

In Perl, the map function is used to apply a given operation to each element of a list or an array, and returns a new list or array containing the results. Here's an example that demonstrates how to use map:

ref‮ re‬to:theitroad.com
#!/usr/bin/perl

use strict;
use warnings;

my @numbers = (1, 2, 3, 4, 5);

my @squared_numbers = map { $_ * $_ } @numbers;

print "Original numbers: @numbers\n";
print "Squared numbers: @squared_numbers\n";

In this example, we define an array of numbers @numbers containing the values 1 through 5. We then use the map function to create a new array @squared_numbers by applying the operation { $_ * $_ } to each element of @numbers. This operation simply squares each element of @numbers.

Finally, we print both the original array @numbers and the new array @squared_numbers to the console. The output of the program should look like this:

Original numbers: 1 2 3 4 5
Squared numbers: 1 4 9 16 25

Note that the map function is a powerful tool in Perl programming, as it allows you to easily transform one array or list into another, based on a set of rules that you define.