1. Perl
  2. hash

Understand Perl hashes

A description of Perl's "hash". Perl hashes are "associative arrays" in other languages.

In an array, you could retrieve an element by specifying a number, but in an "associative array", you can retrieve an element by specifying a string as a key.

Hash basics

The basis of hashing. Hash declaration and initialization. Hash output and assignment. You can think of a hash as an array from which you can get a value of a string as a subscript. You can retrieve the value using a string as a key.

Hash declaration and initialization

Declaring and initializing the hash is done as follows.

my %fathers = ('Taro' =>'Kenji', 'Naoko' =>'Yoshio');
# or
my %fathers = (Taro =>'Kenji', Naoko =>'Yoshio');

The variable prefix is percent sign "%". (Key 1 => Value 1, Key 2 => Value 2, ...). Taro's father can be read as Kenji, Naoko's father as Yoshio

Hash element output

print $mothers{'Taro'};

When printing, $instead of%. $When dealing with a single element. Use {} to specify the key. (In the case of an array, it was []). {} Is read as a bracket. ([] Is brace. () Is parenthesis)

Hash element assignment

$mothers{'Naoko'} = 'Haruna';

Processing of each element of hash

Hash manipulation

Hash reference

You can create a hash reference as follows:

my $math_scores_ref = \%math_scores;

Prefix%with\to generate a reference to the hash. Assign the generated reference to the scalar variable $math_score_href

Create a direct hash reference using an anonymous hash.

my $math_scores_ref2 = {
  Aiko => 89,
  Kenta => 100,
  Taro => 34,
};

You can use {} instead of () to create a reference to a direct hash

Access the hash element

$math_scores_ref->{Aiko}

Extract the hash from the hash reference

Dereference to retrieve a hash from a hash reference.

my %hash = %$hash_ref;
my %hash = %{$hash_ref};

You can also dereference a reference to a hash to retrieve the hash.

my %math_scores = %$math_scores;

You can dereference and retrieve hashes with%$math_scores.

You can use "->{key}" if you only need one element.

$math_scores->{Taro};

You can read more about hash reference here.

Multidimensional data structure using hash

The multidimensional data structure using hashes is explained in detail in the following articles. You can learn about "array hash", "hash array", and "hash hash".

Column "It's Perl's fault that associative arrays are called hashes"

Perl has made a great decision to incorporate associative arrays as a feature of the language. It's incredibly convenient. This was a huge success, and almost all other scripting languages have been affected to incorporate associative arrays into the language itself.

Perhaps the name associative array was long, Perl decided to call it a hash. This is an association from the hash function.

But, surprisingly, it seems to represent the data structure exactly.

Example code

Hash basics

The basis of hashing. Hash declaration and initialization. This is an example of hash output and assignment.

use strict;
use warnings;

# What is a hash?
# You can think of the string as a subscript and an array from which you can get the value.
# You can retrieve the value using the string as a key.

# Hash declaration and initialization (variable prefix is%)
# (Key 1 => Value 1, Key 2 => Value 2, ...)
# Taro's father reads Kenji, Naoko's father reads Yoshio
my %fathers = ('Taro' =>'Kenji', 'Naoko' =>'Yoshio');

# More Perl-like writing
# The'' to the left of => can be omitted
my %mathers = (Taro =>'Tomoko', Naoko =>'Shizuka');

# 1: Hash element output
print "1: Hash element output\n";
# When outputting, $instead of%
# {} Use brackets
print "\$mathers{'Taro'} =", $mathers{'Taro'}, "\n";
print "\n";

# 2: Hash element assignment
print "2: Hash element assignment\n";
$mathers{'Naoko'} = 'Haruna';
print "\$mathers{'Naoko'} =", $mathers{'Naoko'} . "\n";

Output

1: Hash element output
$mothers{'Taro'} = Tomoko

2: Assignment of hash elements
$mothers{'Naoko'} = Haruna

Hash reference

This is an example using the hash reference.

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

# Student math scores
my %math_scores = (
  Aiko => 89,
  Kenta => 100,
  Taro => 34,
);

# 1: Hash reference%is preceded by\to generate a reference to the hash.
# Assign the generated reference to the scalar variable $math_scores_ref
my $math_scores_ref = \%math_scores;
print "1: Hash reference\n";
print Data::Dumper->Dump([$math_scores_ref], ['$math_scores_ref']);
print "\n\n";

# 2: Make a direct hash reference using an anonymous hash ({}).
my $math_scores_ref2 = {
  Aiko => 89,
  Kenta => 100,
  Taro => 34,
};

print "2: Anonymous hash\n";
print Data::Dumper->Dump([$math_scores_ref2], ['$math_scores_ref2']);
print "\n\n";

# 3: Access the hash element. ($hash_ref->{key})
print "3: Access the hash element.\n";
print "\$math_scores_ref->{Aiko} = $math_scores_ref->{Aiko}\n";
print "\$math_scores_ref->{Kenta} = $math_scores_ref->{Kenta}\n";

Output

1: Hash reference
$math_scores_ref = {'Aiko' => 89,
                     'Kenta' => 100,
                     'Taro' => 34
                   };


2: Anonymous hash
$math_scores_ref2 = {
                      'Aiko' => 89,
                      'Kenta' => 100,
                      'Taro' => 34
                    };


3: Access the hash element.
$math_scores_ref->{Aiko} = 89
$math_scores_ref->{Kenta} = 100

Hash reference dereference

This is an example to dereference the hash reference.

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

# Student math scores
my $math_scores = {
  Taro => 89,
  Naoko => 54,
  Kenji => 54
};

# 1: Dereference and output all elements of the hash
print "1: Dereference and print all elements of the hash\n";
for my $key (keys %$math_scores) {
  print "\$math_scores->{$key} =" . $math_scores->{$key} . "\n";
}
print "\n";

# 2: Use the->operator to refer to an element.
print "2: Delete one key\n";
delete $math_scores->{Taro};

print Data::Dumper->Dump([$math_scores], ['$math_scores']);

Output

1: Dereference and output all elements of the hash
$math_scores->{Kenji} = 54
$math_scores->{Taro} = 89
$math_scores->{Naoko} = 54

2: Delete one key
$math_scores = {
                 'Kenji' => 54,
                 'Naoko' => 54
               };

Related Informatrion