An example of perl hash Analysis

Source: Internet
Author: User

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

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.