Copy Code code 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 ',
);
#输出哈希的值, the result of the output is Kubrick
Print $movies {' The Shining '};
#同时输出键和值
foreach $film (keys%movies)
{
Print "$film is directed by $movies {$film}.\n";
}
#添加空格
print "\ n";
#哈希结构的切换
%reverse_result=reverse%movies;
foreach $director (keys%reverse_result)
{
Print "$director directe the $reverse _result{$director}.\n";
}
#添加空格
print "\ n";
#当哈希结构用于列表环境中时, Perl changes the hash back to a normal list of keywords and key values
@data =%movies;
print "@data \ n";
#添加空格
print "\ n";
#得到的数组是一个分为奇数为film, the even number is an array of director, or the opposite
#然后我们将数组赋值给hash
%movies= @data;
foreach $director (keys%reverse_result)
{
Print "$director directe the $reverse _result{$director}.\n";
}
Print "The result is not change\n";
The following are the output results:
f:\>perl\a.pl
Kubrickgoonies was directed by Spielberg.
The Shining was directed by Kubrick.
Ten Commandments is 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:\>
#----The method to test the hash key:
If (exists $hash {keyval})
{
}
#----Delete the keyword:
Delete hash {keyval};
#---empty hash:
%hash= ();