Parsing Perl hash table usage

Source: Internet
Author: User


This article focuses on the concept of a Perl hash table. The Perl language and other programming languages have different features. Here we will share with you the concept of a Perl hash table, in fact, a Perl hash table is a structure.

Perl hash table
A Perl hash table is a structure.
Key/value.
Access Perl Hash Table Elements
$ Perl hash table {$ some_key}
When selecting a name for a Perl hash table, it is best to think like this: the names and keys of the elements in the Perl hash table can be connected using. For example, thefamily_nameforfredisflintstone.

To reference the entire Perl hash table, use the percent sign (%) as the prefix.


The Code is as follows :#! /Bin/perl
Usewarnings;
Usestrict;


My $ person;
My % family_name;
$ Family_name {"fred"} = "flintstone ";
$ Family_name {"barney"} = "rubble ";

Foreach $ person (qw <barneyfred> ){
Print "I 'veheardof $ person $ family_name {$ person}. \ n ";
}
My % some_hash = ("foo", 35, "bar", 12.4, 25, "hello", "wilma", 1.72e30, "betty", "bye \ n ");
My @ array_array = % some_hash =;
Print "@ array_array \ n ";

Hash Value assignment (=>)


The Code is as follows: my % last_name = (
"Fred" => "flintstion ",
"Dino" => undef,
"Barney" => "rubble ",
"Betty" => "rubble ",
);

The keys function returns all keys of this Perl hash table, and values includes tax. If there are no elements in the Perl hash table, this function returns an empty list.


The Code is as follows: my @ k = keys % last_name;
My @ v = values % last_name;
My $ count = keys % last_name; # scalar-producing, key/valuepairs


Print "thekeyare @ k. \ n ";
Print "thevalueare @ v. \ n ";
Print "thecountare $ count. \ n ";

Each function

To iterate to get each element in the Perl hash table, a common method is to use the each function, which returns the element pair of the key/value pair. When an iteration is performed on the same Perl hash table function, the next key/value pair is returned until all elements are accessed.

The Code is as follows: my $ key;
My $ value;
While ($ key, $ value) = each % last_name ){
# Foreach ($ key, $ value) = each % last_name ){
Print "$ key => $ value. \ n ";
}

Note that the two cycles have different design mechanisms.

The Code is as follows: foreach $ key (sortkeys % last_name ){
$ Value = $ last_name {$ key };
Print "$ key => $ value. \ n ";
Print "$ key => $ last_name {$ key}. \ n ";
}

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.