Perl hash
last modified October 8, 2026
Perl hash tutorial shows how to work with hashes in Perl.
A hash is an associative array of scalars: each string key identifies one value. Hash keys are unique, and Perl does not guarantee a particular order when it returns the keys or values.
A hash is a basic Perl data type. A data type is a set of values and operations that can be done with these values.
In other programming languages such as C# or Python, a hash is often called a dictionary.
Perl uses the % sigil to define a hash.
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
The => (fat comma) operator separates keys and values in a hash
literal. It also quotes a bareword on its left, so quotes are optional for
identifier-like keys.
Perl simple hash
In the following example, we work with a simple hash.
#!/usr/bin/perl
use warnings;
use 5.30.0;
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
say $words{0};
say $words{1};
say $words{5};
say '-----------------';
say map { "$_ $words{$_}\n" } sort keys %words;
say '-----------------';
my @keys = sort keys %words;
my @values = @words{@keys};
say join ' ', @keys;
say join ' ', @values;
In the example, we define a hash of words. We print individual values, key/value pairs and keys and values of the hash.
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
A Perl hash of words is defined. The hash uses the % character.
say $words{0};
We print the value that is stored with key 0. When we refer to an individual
value of a hash, we use the $ character and a pair of curly
{} brackets.
say map { "$_ $words{$_}\n" } sort keys %words;
We sort the keys before printing so the key/value pairs appear in a stable order; hash iteration order is otherwise unspecified.
my @keys = sort keys %words;
The keys function returns the keys. Their order is not guaranteed,
so we sort them for this example.
my @values = @words{@keys};
The hash slice gets values in the same sorted-key order, keeping each value paired with its key.
$ ./simple.pl sky tomorrow forest ----------------- 0 sky 1 tomorrow 2 blue 3 pink 4 work 5 forest ----------------- 0 1 2 3 4 5 sky tomorrow blue pink work forest
Perl hash size
In the following example, we determine the size of the hash.
#!/usr/bin/perl
use warnings;
use 5.30.0;
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
my $n = keys %words;
say "the hash has $n items";
To get the size of a hash, use keys in scalar context; it returns
the number of entries.
$ ./hash_size.pl the hash has 6 items
Perl add and remove hash elements
In the next example we show how to add and remove elements in a hash.
#!/usr/bin/perl
use warnings;
use 5.30.0;
use Data::Dumper;
my %words = ();
$words{0} = 'sky';
$words{1} = 'rock';
$words{2} = 'bed';
say Dumper(\%words);
delete $words{2};
say '---------------------';
say Dumper(\%words);
%words = ();
say '---------------------';
say Dumper(\%words);
First, we initialize an empty hash. We add three pairs to the hash, remove
one pair, and finally clear the hash. We show the contents of the hash
with Data::Dumper.
my %words = ();
An empty hash is initialized.
$words{0} = 'sky';
$words{1} = 'rock';
$words{2} = 'bed';
We add three key/value pairs to the hash.
delete $words{2};
A pair is removed with delete function.
%words = ();
We clear the hash.
$ ./add_remove.pl
$VAR1 = {
'2' => 'bed',
'1' => 'rock',
'0' => 'sky'
};
---------------------
$VAR1 = {
'1' => 'rock',
'0' => 'sky'
};
---------------------
$VAR1 = {};
Perl hash iterate
We can iterate over hash keys, values, or key/value pairs with
for and foreach. Hash iteration order is not guaranteed;
sort keys when a stable order is needed.
#!/usr/bin/perl
use warnings;
use 5.30.0;
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
foreach my $v (sort values %words){
say $v;
}
say '----------------';
foreach my $k (sort keys %words){
say $k;
}
say '----------------';
for (sort keys %words) {
print("$_: $words{$_}\n");
}
In the example, we iterate over the hash of words.
$ ./iterate.pl blue forest pink sky tomorrow work ---------------- 0 1 2 3 4 5 ---------------- 0: sky 1: tomorrow 2: blue 3: pink 4: work 5: forest
Perl hash sort
Hash entries have no defined traversal order. The sort function
can sort keys or values; sorting values alone does not preserve their association
with keys.
#!/usr/bin/perl
use warnings;
use 5.30.0;
my %words = (0=>'sky', 1=>'tomorrow', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
foreach my $v (sort values %words){
say $v;
}
say '----------------';
foreach my $v (reverse sort values %words){
say $v;
}
say '----------------';
foreach my $k (sort keys %words){
say $k;
}
say '----------------';
foreach my $k (reverse sort keys %words){
say $k;
}
In the example, we use the sort and reverse functions
to sort the keys and values independently in ascending and descending order.
$ ./sorting.pl blue forest pink sky tomorrow work ---------------- work tomorrow sky pink forest blue ---------------- 0 1 2 3 4 5 ---------------- 5 4 3 2 1 0
Perl compare hashes
The non-core Data::Compare CPAN module can compare Perl data
structures (install it with cpanm Data::Compare).
#!/usr/bin/perl
use warnings;
use 5.30.0;
use Data::Compare;
my %h1 = (
0 => ['a', 'b', 'c'],
1 => ['d', 'e', 'f'],
);
my %h2 = (
0 => ['a', 'b', 'c'],
1 => ['d', 'e', 'f'],
);
if (Compare(\%h1, \%h2)) {
say 'the two hashes are equal';
} else {
say 'the two hashes are not equal';
}
The example compares two hashes with Data::Compare.
if (Compare(\%h1, \%h2)) {
The Compare function takes references to the two data structures.
$ ./comparing.pl the two hashes are equal
Perl hash slice
It is possible to extract a list of values from a hash.
#!/usr/bin/perl
use warnings;
use 5.30.0;
my %words = (0=>'sky', 1=>'cup', 2=>'blue', 3=>'pink',
4=>'work', 5=>'forest');
my ($first, $second) = @words{(0, 1)};
say $first;
say $second;
In the example, we take two words from a list of words.
my ($first, $second) = @words{(0, 1)};
We use the @ sigil to get the slice.
$ ./hash_slice.pl sky cup
In this article we have worked with hash data type in Perl.
Author
List all Perl tutorials.