Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 43 ยท Arrays and Hashes

List Functions and Sorting

Learn Perl's list functions: map, grep, sort with custom comparisons, the Schwartzian transform, reverse, join and the List::Util helpers first, sum, max and uniq.

Perl programs spend much of their time transforming lists: pick the items that match, convert each one, put them in order, glue them into a string. A handful of built-in functions do almost all of this work, and once you are fluent in them, loops that took ten lines become one expressive line. They are map, grep, sort, reverse and join, plus the helpful List::Util module that ships with Perl.

All of these functions take a list in and give a list (or a single value) out, and none of them modify the original array. If you have not read Arrays yet, do that first. The idea of context from Context: Scalar vs List shows up again here, because grep returns a count in scalar context.

Perl is not installed on this site, so the Perl listings are read-only; their outputs come from real runs of Perl 5.42. The TryIt blocks use Python as an honest stand-in that follows the same logic.

map: transform every element

map BLOCK LIST runs the block once for each element, with the element in $_, and collects whatever the block returns. The result is a new list, and the block may return zero, one or several values per element:

PERL
use v5.36;

my @n  = (1 .. 10);
my @sq = map { $_ * $_ } @n;                  # 1 4 9 16 ... 100
my @names = map { ucfirst lc } qw(aLICE bOB); # Alice Bob

my @flat = map { ($_, $_ * 10) } 1 .. 3;      # 1 10 2 20 3 30

map is also the standard way to build a hash from a list. Return a key and a value for each element:

PERL
my %len = map { $_ => length $_ } qw(pear fig banana);
# (pear => 4, fig => 3, banana => 6)

Perl has to guess whether a { after map starts a block or an anonymous hash. When it guesses wrong, say what you mean: +{ ... } is a hash and {; ... } is a block. Hashes themselves are the subject of Hashes.

grep: keep the matching elements

grep BLOCK LIST keeps the elements for which the block is true. It also takes a plain regex as a shortcut, as in grep /a/, @words:

PERL
my @even = grep { $_ % 2 == 0 } @n;   # 2 4 6 8 10
my $count = grep { $_ > 5 } @n;       # 5  (scalar context: the number kept)
my @idx = grep { $words[$_] =~ /^[A-Z]/ } 0 .. $#words;   # matching indexes

In scalar context grep returns how many elements matched, which makes it a one-line "how many" check. Because $_ is an alias, modifying it inside map or grep modifies the original array, so avoid assigning to $_ there.

Both functions chain naturally. Read the pipeline from right to left: take the numbers, keep the odd ones, double them:

PERL
my @doubled_odds = map { $_ * 2 } grep { $_ % 2 } @n;   # 2 6 10 14 18
Python map and grep (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

sort: put things in order

sort returns a sorted copy. Without a block it compares as strings, so uppercase letters come before lowercase ones and 100 comes before 9:

PERL
my @words = qw(pear Apple fig banana Cherry);
say join " ", sort @words;                 # Apple Cherry banana fig pear
say join " ", sort (10, 9, 2, 33, 1);      # 1 10 2 33 9
say join " ", sort { $a <=> $b } qw(10 9 2 33 1);   # 1 2 9 10 33

With a block, you decide. Inside the block $a and $b are the two elements being compared, and the block returns a negative number, zero or a positive number. That is exactly what <=> (numbers) and cmp (strings) give you, as described in Operators and Numbers. Swap the operands for descending order:

PERL
sort { $a <=> $b } @n        # ascending numbers
sort { $b <=> $a } @n        # descending numbers
sort { lc($a) cmp lc($b) } @words    # case-insensitive text

To sort by several keys, chain comparisons with or. The second comparison is only used when the first says the items are equal:

PERL
say join " ", sort { length($a) <=> length($b) or $a cmp $b } @words;
# fig pear Apple Cherry banana

Sorting records works the same way. Here each record is an array reference holding a name and an age (more on these in References):

PERL
my @recs = (["bob", 25], ["alice", 30], ["carl", 25]);
say join " ", map { $_->[0] }
              sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } @recs;
# bob carl alice

Never use $a or $b as your own variable names: sort sets them, and my $a breaks it.

The Schwartzian transform

If the sort key is expensive to compute (the length of a string is cheap, but reading a file's modification time is not), a normal sort block recomputes it for every comparison. The Schwartzian transform computes each key once: build pairs of (key, original), sort the pairs, then throw the keys away. Read it from bottom to top:

PERL
my @sorted = map  { $_->[1] }                         # 3. keep the originals
             sort { $a->[0] <=> $b->[0]
                    or $a->[1] cmp $b->[1] }          # 2. sort by key
             map  { [length($_), $_] } @words;        # 1. attach the key
# fig pear Apple Cherry banana

Three chained list operations, each feeding the next, with no temporary variables. It is named after Randal Schwartz, and every Perl programmer is expected to recognise it.

Python Custom sorting and the Schwartzian transform (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

reverse and join

reverse returns a list in the opposite order. join glues a list into one string with a separator, and is the opposite of split:

PERL
say join ", ", reverse @words;          # Cherry, banana, fig, Apple, pear
say join "-", @words;                   # pear-Apple-fig-banana-Cherry
say join " ", reverse sort { $a <=> $b } (5, 3, 11, 1);   # 11 5 3 1

Remember from the lesson on context that reverse in scalar context reverses characters instead, so use scalar reverse for strings and plain reverse for lists.

List::Util: the standard helpers

Many list tasks come up so often that Perl ships a module for them. List::Util is a core module, so it is available everywhere Perl is. Import the functions you want:

PERL
use List::Util qw(first sum sum0 max min maxstr uniq reduce any all none);

my @n = (1 .. 10);
say sum(@n);                    # 55
say max(@n), " ", min(@n);      # 10 1
say sum0();                     # 0  (sum of an empty list; sum() would give undef)
say maxstr(@words);             # pear
say first { $_ > 4 } @n;        # 5  (the first match, or undef)
say join ",", uniq(1,1,2,3,3,3,4);   # 1,2,3,4
say reduce { $a * $b } 1 .. 5;  # 120
say "any"  if any  { $_ == 3 } @n;
say "all"  if all  { $_ > 0 }  @n;
say "none" if none { $_ > 10 } @n;

first stops at the first match, so it is faster than grep when you only need one element, and it returns undef if nothing matches. reduce folds a list into one value by repeatedly combining two items ($a and $b again). uniq removes duplicates while keeping the first occurrence of each value in order. The any, all and none functions need a recent version of List::Util; they come with every modern Perl.

A rule of thumb: use grep when you want all the matches, first when you want one, and any when you only need a yes or no.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does sort (10, 9, 2, 33, 1) return without a block?

    The default sort compares strings, so "10" comes before "2". Use sort { $a <=> $b } for numeric order.
  2. 2. What does my $n = grep { $_ > 5 } (1 .. 10); store in $n?

    In scalar context grep returns the number of elements for which the block was true.
  3. 3. How do you sort strings from longest to shortest, using alphabetical order to break ties?

    Swapping $a and $b in the first comparison gives descending length. The or runs the alphabetical comparison only when the lengths are equal.
  4. 4. Why use the Schwartzian transform?

    It attaches the computed key to each element, sorts those pairs, then discards the keys, so the key function runs once per element instead of once per comparison.
  5. 5. Which List::Util function returns the first element that satisfies a condition, or undef?

    first stops at the first element where the block is true and returns undef when none matches.

Summary

  • map transforms each element and may return several values per element; grep filters and counts in scalar context.
  • sort compares as strings by default; use { $a <=> $b } for numbers and chain keys with or.
  • The Schwartzian transform (map, sort, map) computes expensive sort keys once.
  • reverse and join rearrange and format lists; reverse behaves differently in scalar context.
  • List::Util gives you sum, max, min, first, uniq, reduce, any, all and none for free.

Next: Hashes