Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 43 ยท Arrays and Hashes

Hash Patterns

Learn the hash idioms every Perl programmer uses: frequency tables, grouping with array refs, inverting, sets, removing duplicates, sorting by value and key order.

Once you know how a hash works, a small set of recurring patterns will solve most of the problems you meet. Counting how often things occur, grouping items by a category, flipping a lookup table around, testing membership, removing duplicates and ranking by value: each one is only a line or two of Perl, and each is something you will write again and again.

This lesson is a tour of those patterns, using the hash basics from Hashes and the list tools from List Functions and Sorting. A couple of the grouping examples use array references (@{ ... }), which are explained properly in References; here you only need to copy the shape.

Perl is not installed on this site, so the Perl listings are read-only; their outputs come from real runs of Perl 5.42. Every pattern has a Python stand-in in a TryIt block that you can run and change.

Five patterns, one data type

Count $h{$x}++
Group push @{ $h{$k} }, $x
Invert reverse %h
Set exists $h{$x}

Sort the keys whenever the order matters.

1 / 2

Frequency tables

Counting is the pattern behind word counts, log analysis and histograms. The hash key is the thing you count, and the value is the tally. Then sort by the count, highest first, and use the key as a tie-breaker so the output is stable:

PERL
use v5.36;

my $text = "the cat and the dog and the bird";
my %freq;
$freq{$_}++ for split ' ', $text;

for my $w (sort { $freq{$b} <=> $freq{$a} or $a cmp $b } keys %freq) {
    say "$w $freq{$w}";
}
# the 3
# and 2
# bird 1
# cat 1
# dog 1

The sort block compares the values ($freq{$b} <=> $freq{$a}, with $b first for descending order) and falls back to the key with or $a cmp $b when the counts tie. Without that second comparison, tied words would appear in whatever order the hash happened to use.

To get only the top few, slice the sorted list: (sort { ... } keys %freq)[0..1] gives the and and.

Python Frequency table, most common first (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.

Grouping

Grouping sorts items into categories. Each value is now a list of items, so the hash needs to store an array reference. Perl creates the array automatically the first time you push to it (this is called autovivification):

PERL
my @people = (["ann","dev"], ["bo","ops"], ["cy","dev"], ["di","qa"], ["ed","ops"]);

my %by_role;
push @{ $by_role{ $_->[1] } }, $_->[0] for @people;

for my $role (sort keys %by_role) {
    say "$role: @{ $by_role{$role} }";
}
# dev: ann cy
# ops: bo ed
# qa: di

Read @{ $by_role{$role} } as "the array that this value points to". The same shape groups anything: files by extension, orders by customer, words by length. The key can be computed from the item, as in push @{ $by_len{ length $_ } }, $_ for @words;.

You can also keep a running total per group, which needs no references at all:

PERL
my %sum;
$sum{ $_->[0] } += $_->[1] for (["x", 1], ["y", 2], ["x", 5]);
# x=6, y=2

Inverting a hash

reverse on a hash swaps keys and values, because the hash flattens into a list and the list is read back in the opposite order:

PERL
my %id_of   = (a => 1, b => 2);
my %name_of = reverse %id_of;        # (1 => "a", 2 => "b")

This is perfect for a lookup table you want to search in both directions, as long as the values are unique. If two keys share a value, one of them is lost. When values can repeat, build an inverted hash of groups instead, using the grouping pattern:

PERL
my %color = (apple => "red", banana => "yellow", cherry => "red");

my %by_color;
push @{ $by_color{ $color{$_} } }, $_ for sort keys %color;

say join "; ", map { "$_: @{ $by_color{$_} }" } sort keys %by_color;
# red: apple cherry; yellow: banana
Python Grouping and inverting (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.

Sets: membership, intersection, difference

Perl has no set type, but a hash does the job. Store each member as a key (the value is just 1) and test membership with exists or a plain boolean check, in constant time:

PERL
my %set   = map { $_ => 1 } qw(a b c);
my %other = map { $_ => 1 } qw(b c d);

say exists $set{b} ? "in" : "out";                 # in

my @both  = sort grep {  $set{$_} } keys %other;   # b c   (intersection)
my @only  = sort grep { !$other{$_} } keys %set;   # a     (difference)
my @union = sort keys %{{ %set, %other }};         # a b c d

The union works because merging two hashes keeps one copy of each key. The odd-looking %{{ ... }} means "build an anonymous hash from this list, then treat it as a hash".

Removing duplicates

A "seen" hash removes duplicates while preserving the original order. The post-increment returns the old value (undef, which is false, the first time), so !$seen{$_}++ is true exactly once per value:

PERL
my @dups = (1, 2, 2, 3, 3, 3);
my %seen;
my @unique = grep { !$seen{$_}++ } @dups;   # 1 2 3

If you only need the unique values and do not care about extras, List::Util::uniq does the same job with a clearer name (see List Functions and Sorting). The hash version generalizes: you can key on any part of a record, for example grep { !$seen{ $_->{email} }++ } @users.

Sorting by value

A hash cannot be sorted in place, but you can sort its keys by the value stored under each key, which gives a ranking:

PERL
my %score = (ann => 90, bo => 72, cy => 90, di => 85);

say join " ", map { "$_($score{$_})" }
              sort { $score{$b} <=> $score{$a} || $a cmp $b } keys %score;
# ann(90) cy(90) di(85) bo(72)

my ($best) = sort { $score{$b} <=> $score{$a} || $a cmp $b } keys %score;
say $best;    # ann

Notice the list assignment my ($best) = ...: it keeps only the first element, a use of the context rules from Context: Scalar vs List. If you only want the highest value and not the name, max values %score from List::Util is simpler.

Python Sets, de-duplication and ranking (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.

Order of keys

The last pattern is really a warning. A Perl hash has no meaningful order: since Perl 5.18, the order of keys, values and each is deliberately randomized and can change every time your program starts. Two consequences:

  • Never depend on hash order for output, tests or comparisons. Sort the keys (sort keys %h), or sort by value as shown above.
  • Within one run, keys %h and values %h are consistent with each other, so a hash can be copied into parallel lists safely.

If you genuinely need insertion order, keep a separate array of keys next to the hash, or use a module such as Tie::IxHash from CPAN. Python dictionaries, used in the stand-ins above, do remember insertion order, which is one more reason the sort calls were written explicitly.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which line builds a frequency table of the words in @words?

    ++ on a missing key starts from undef (treated as 0), so each occurrence adds one to that word's tally.
  2. 2. What is stored in $h{$k} after push @{ $h{$k} }, $x; runs for the first time with a new key?

    Perl autovivifies an array reference under the new key, then pushes $x onto that array.
  3. 3. What does %name_of = reverse %id_of lose if two keys share the same value?

    After the swap the shared value becomes a key twice, and the later pair overwrites the earlier one.
  4. 4. What does grep { !$seen{$_}++ } @list return?

    The post-increment returns the old count, which is false (undef) the first time a value appears, so only the first occurrence passes the test.
  5. 5. Why does the ranking code use or $a cmp $b after comparing the scores?

    Hash order is not guaranteed, so without a tie-breaker, equal scores could appear in a different order on each run.

Summary

  • Count with $h{$x}++, then rank with sort { $h{$b} <=> $h{$a} or $a cmp $b } keys %h.
  • Group with push @{ $h{$key} }, $item, which creates each array the first time it is needed.
  • reverse %h inverts a hash when the values are unique; group instead when they are not.
  • A hash with 1 as every value is a set, and !$seen{$_}++ removes duplicates in order.
  • Hash order is random between runs, so always sort keys when output order matters.

Next: Dates and Times