An example analysis of the Perl hash _perl

Source: Internet
Author: User
Tags hash

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= ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.