Magic Perl-hash and array

Source: Internet
Author: User

Statement: This magic Perl series for Liu Xing (http://deepfuture.iteye.com/) original, without the author's authorization, no one and institutions can reprint 1. Hash embedded Array

The syntax format is as follows:

Declaration and definition

My % hash variable = (

Hash key 1 => [array element 1, array element 2 ,..., Array Element N],

Hash key 2 => [array element 1, array element 2 ,..., Array Element N],

......

......

Hash key N => [array element 1, array element 2 ,..., Array Element N]

);

Access a single element of an embedded array:

$ Hash variable name {hash key}-> [array subscript]

Access the embedded array:

@ {$ Hash variable name {hash key }}

For example, a game map contains the coordinates of a group of buildings as follows:

Weapons store:

Training venue: 85,196

Equipment stores:

Magic store:, 45

Use a hash embedded array to store and access the data.

1) Storage

My % game = (

"Weapons store" => [123,35],

"Training ground" => [85,196],

"Clothing store" => [],

"Magic store" => []

);

2) Access

Coordinates for accessing the device store:

Printf ("% d, % d", @ {$ game {"defense shop "}});

Coordinate Y value for accessing the magic store:

Print $ game {"device store"}-> [1];

You can use a hard quote to assign values to an embedded array.

My % game;

My @ array = (123,35 );

$ Game {"weapons store" }=\@ array;

My @ array = (85,196 );

$ Game {"training ground" }=\@ array;

My @ array = (67,96 );

$ Game {"clothing store" }=\@ array;

My @ array = (67,96 );

$ Game {"magic store" }=\@ array;

Printf ("% d, % d", @ {$ game {"defense shop "}});

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.