Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Sep 102010
 

Generate new list using map in Perl

Answer:

The Perl's map function is superb cool for generating new list based on an existing list.

1. Generate new array based on an existing array

my @new = map {$_ * 2} (1, 2, 3);

# @new contains 2, 4, 6

2. Generate new hash based on an existing array

my %new = map {$_ => 1} (1,2,3);

# %new contains a hash...
#      {
#           '1' => 1,
#           '3' => 1,
#           '2' => 1
#       };


 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>