How to print hash in Perl

It is sometimes necessary to print the content of a hash in Perl.

For example, there is a hash %h with the following content:

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

Depending on what you want to, there are a several different ways to print the hash.

Using Data::Dumper

If you need to quickly see the contents of a hash, you can use Perl module Data::Dumper. It can be useful when you are developing or debugging Perl program.

Here is an example:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

print Dumper \%h;

This is the output of the program:

$VAR1 = {
          'Bob' => 'yellow',
          'Alice' => 'silver',
          'John' => 'red'
        };

Hash stores key-value pairs. But it does not store the order of that pairs. If you run this program several times you will see the pairs in different order.

Using print

What happens if you pass the hash to print?

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

print %h;

The program displays one line:

JohnredBobyellowAlicesilver

You can see that print outputs all key-value pairs together, without any separator. Different runs of the program will give different output (because the hash does not store the order of its key-value pairs).

print and variable $,

If you specify print %h;, it will print all the key-value together. But you can ask perl to insert a symbol between the elements. For example, use can use the new line character \n.

To do it you need to place that symbol in a special variable $,. Here is the code:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

$, = "\n";

print %h;

One of the possible results of the program (the data in the hash is not ordered, so different runs will output the pairs in a different order):

Bob
yellow
Alice
silver
John
red

But you need to use this method with caution. Variable $, is a global variable. When you change it it is changed for the entire program. In some situations makes sense not to use this method, or to store the value of this variable in a temporary variable, then print, and then reassign the previous value to the variable. Something like this:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

my $saved = $,;

$, = "\n";

print %h;

$, = $saved;

Iterating over hash

The most powerful and flexible way to output the content of hash on the screen is go through all the values in the hash and print them on the in the needed form. Here is one example of how this can be done:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;
use feature qw(say);

my %h = (
    John => 'red',
    Alice => 'silver',
    Bob => 'yellow',
);

foreach my $name (sort keys %h) {
    say "$name $h{$name}";
}

The output of the program is always the same:

Alice silver
Bob yellow
John red

For order of pairs in the hash is undefined, but this program uses sort to display the keys in alphabetical order.

Related topics

Other articles