Perl Hash Table Usage parsing _perl

Source: Internet
Author: User
Tags hash

This article is focused on the Perl hash table concept, Perl and other programming languages have their own characteristics, here and you share the concept of Perl hash table, in fact, Perl hash table is a structure.

Perl Hash Table
A Perl hash table is a structure.
Key/value.
Accessing Perl hash Table elements
$Perl Hash Table {$some _key}
When choosing a name for a Perl hash table, it's best to think about it: The Perl hash Table element's name and key can be connected by using for. such as Thefamily_nameforfredisflintstone.

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

Copy Code code 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", "," Bar ", 12.4,25," Hello "," Wilma ", 1.72e30," Betty "," bye\n ");
my@array_array=%some_hash=;
print "@array_array \ n";

Hashi Value Method large arrow symbol (=>)

Copy Code code as follows:

My%last_name= (
"Fred" => "Flintstion",
"Dino" =>undef,
"Barney" => "rubble",
"Betty" => "rubble",
);

The keys function returns all values for this Perl hash table with all keys,values tax-included. If there are no elements in the Perl hash table, this function returns an empty list.

Copy Code code 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

If you want to iterate through each element of the Perl hash table, a common method is to use the each function, which returns the element pairs 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.

Copy Code code as follows:

My$key;
My$value;
while (($key, $value) =each%last_name) {
#foreach (($key, $value) =each%last_name) {
Print "$key => $value. \ n";
}

Notice the results of the two loops because of the different design mechanisms of the two loops.
Copy Code code 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.