Skip to content

Perl Map and Grep Guide — List Transformation and Filtering Functions

DodaTech Updated 2026-06-28 3 min read

In this tutorial, you will learn about Perl Map and Grep Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl map and grep are functional list-processing operators -- map transforming each element via an expression into a new list, and grep filtering elements by a boolean condition -- often chained for concise Data Pipelines without explicit loops.

Basic grep

my @numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

# Filter even numbers
my @evens = grep { $_ % 2 == 0 } @numbers;  # (2, 4, 6, 8, 10)

# Filter by string length
my @long = grep { length($_) > 5 } qw(cat elephant dog giraffe);
# ("elephant", "giraffe")

Basic map

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

# Square each number
my @squares = map { $_ ** 2 } @numbers;  # (1, 4, 9, 16, 25)

# Uppercase strings
my @upper = map { uc($_) } qw(hello world);  # ("HELLO", "WORLD")

# Transform hash to array
my %hash = (a => 1, b => 2);
my @pairs = map { "$_=$hash{$_}" } keys %hash;  # ("a=1", "b=2")

Chaining map and grep

my @data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

# Filter then transform
my @result = map  { $_ * 2 }
             grep { $_ > 5 }
             @data;
# (12, 14, 16, 18, 20)

# Read right to left: start with @data,
# filter those > 5, then multiply by 2

map with Multiple Outputs

my @pairs = (1, 2, 3);

# Map can return multiple elements per input
my @expanded = map { $_, $_ * 10 } @pairs;
# (1, 10, 2, 20, 3, 30)

# Filtering with map (return empty list to remove)
my @filtered = map {
    $_ > 2 ? ($_) : ()
} @pairs;
# (3)

grep in Scalar Context

my @items = qw(apple banana cherry date elderberry);

# Count matches
my $count = grep { length($_) > 5 } @items;
print $count;  # 3

# Boolean - any match?
my $has_long = grep { length($_) > 10 } @items;
print $has_long;  # 0 (false)

Common Mistakes

1. Modifying $_ in map/grep

$_ is aliased to the original element. Modifying $_ changes the original list. Use a copy: my $item = $_.

2. Using grep as boolean without scalar context

if (grep { condition } @list) works in scalar context. But avoid it if you only need to know if any element matches.

3. Expecting map to modify in place

map returns a new list. It does not modify the original. Assign the result: @data = map { $_ * 2 } @data.

Practice Questions

1. What is the difference between map and grep? map transforms each element and returns a new list of the same size. grep filters elements and returns a subset.

2. How do you count elements matching a condition? Use grep in scalar context: my $count = grep { condition } @list.

3. Can map return fewer elements than input? Yes, by returning an empty list () for some elements, effectively filtering during transformation.

FAQ

{{< faq question="Are map and grep faster than loops?" >}} Yes, usually. They are implemented in C and don't create a new scope per iteration. For large lists, they're significantly faster. {{< /faq >}}

{{< faq question="Can I use map on a hash?" >}} Iterate over keys: my @values = map { $hash{$_} } keys %hash. For smaller transformations, map is concise. {{< /faq >}}

{{< faq question="What is $_ in map/grep?" >}} $_ is the current element, aliased to the original list item. It works like an implicit loop variable. {{< /faq >}}

What's Next

Now learn about DBI for database access.

Topic Description Link
DBI Database interface {{< ref "21-dbi" >}}
Debugging Debugging Perl code {{< ref "24-debugging" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro