Copy codeThe Code is as follows :#! /Bin/perl
Use strict;
Use warnings;
My % movies;
My $ film;
My % reverse_result;
My $ director;
My @ data;
% Movies =
(
'The shining' => 'kubrick ',
'Ten Commandments '=> 'demille ',
'Goonies '=> 'spielberg ',
);
# Output the hash value. The output result is Kubrick.
Print $ movies {'the shining '};
# Simultaneously output keys and values
Foreach $ film (keys % movies)
{
Print "$ film was directed by $ movies {$ film}. \ n ";
}
# Adding Spaces
Print "\ n ";
# Hash structure Switching
% Reverse_result = reverse % movies;
Foreach $ director (keys % reverse_result)
{
Print "$ director directe the $ reverse_result {$ director}. \ n ";
}
# Adding Spaces
Print "\ n ";
# When the hash structure is used in the list environment, perl will re-convert the hash to a common list composed of keywords and key values.
@ Data = % movies;
Print "@ data \ n ";
# Adding Spaces
Print "\ n ";
# The obtained array is an array divided into an odd number of film and an even number of ctor, or the opposite
# Then we assign an array to hash.
% Movies = @ data;
Foreach $ director (keys % reverse_result)
{
Print "$ director directe the $ reverse_result {$ director}. \ n ";
}
Print "The result is not change \ n ";
The output result is as follows:
F: \> perl \ a. pl
KubrickGoonies was directed by Spielberg.
The Shining was directed by Kubrick.
Ten Commandments was directed by DeMille.
DeMille directe the Ten Commandments.
Spielberg directe the Goonies.
Kubrick directe the Shining.
Goonies Spielberg The Shining Kubrick Ten Commandments DeMille
DeMille directe the Ten Commandments.
Spielberg directe the Goonies.
Kubrick directe the Shining.
F: \>
# ---- Method for testing the hash key:
If (exists $ hash {keyval })
{
}
# ---- Delete keywords:
Delete hash {keyval };
# --- Clear the hash:
% Hash = ();