An introductory tutorial on the use of Perl hash hashes _perl

Source: Internet
Author: User
Tags hash naming convention

One, what is Perl Hash

A hash is a data structure, and a similar array, that can be stored in or retrieved from a value. However, unlike arrays, the index is not a number, but a name. That is, the index (here, we call it key) is not a number but an arbitrary unique string.

The key can be any string, you can use any string as the key, but they are unique.
Another way of thinking about hashing is to think of it as a bunch of data (a barrel of the data), each with a corresponding label. You can access the element for this label through a label. But there is no concept of the "first" element. In the array, the array elements are numbered from the 0,perl learning hash hash_ Ma Liang computer Technology 1,2. But in the hash, there is no definite order, so there is no first element. Just a collection of some key/value pairs.

Keys and values are any scalar, but the keys are usually converted to strings. Therefore, if the expression 50/20 is used as the keys, it is typically converted to a 3-character string "2.5".
Because of Perl's "No unnecessary restrictions" philosophy of design: hash can be arbitrary size, from empty hash (no key/value pair) to any size that your memory allows.
The keys are unique, but values can be repeated. The value of a hash can be a number, a string, a undef, or a mixture of them, but the key is unique.

Why Use Perl Hash

You can think of a hash as a simple database, where you can have a piece of data under each key. If your task is about: "Query repeat", "unique", "cross-reference", "Query table", then hash is likely to help Perl learn the hash hash_ Ma Liang computer technology You are busy in this kind of application.

Second, access to Perl hash elements

To access the hash element, you can use the following syntax: $hash {$some _key}
This is similar to the method of accessing array elements, where the curly braces ({}) are used instead of square brackets ([]) on the subscript (key). Now the key's expression is a string, not a number.
The name of the hash is the same as the naming convention for other identifiers in Perl (Letters, numbers, underscores, but not numbers).
Accessing a nonexistent hash element will get undef.

1. Perl Hash as a whole

To refer to the entire hash, use the percent sign (%) as the prefix.
For convenience, hash can be converted to a list, or vice versa.

2.Perl Hash Assignment

You can use the following syntax to copy between hashes:
%new_hash =%old_hash;
It is more common to turn hashes into other forms. For example, we can reverse the hash:
%inverse_hash = Reverse%any_hash;

3. Large arrow sign (=>)

When assigning a value to a hash, it is sometimes not obvious which elements are keys, which are values, and therefore the large arrow symbol (=>) is invented. When you need commas, you can replace them with a large arrow symbol.
Such as:

Copy Code code as follows:

My%last_name = (
"www" => 1,
"Eee" => 2,
);

Third, Perl hash function

Some useful functions can operate on the entire hash.

1.keys and Values functions

The keys function returns all values for all keys,values functions that return this hash. If there are no elements in the hash, this function returns an empty list.

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3);
Print my @k = Keys%hash;
Print my @v = values%hash;

2.each function

If you want to iterate over each element of the hash, a common method is to use each function, which returns a list of the 2 elements that key/value corresponds to.
When an iteration is performed on the same hash function, the next Key/value pair is returned until all elements are accessed. If there are no more key/value pairs, the each function returns an empty table.

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3);
while ($key, $value) = each%hash)
{
Print "$key => $value \ n";
}

Of course, each returned a Key/vlaue pair, and the order is messy (it is the same order as the keys and values functions). If you want to discharge them in order, you can sort them (using sort).

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3, "D" =>4);
foreach $key (sort keys%hash)
{
$value = $hash {$key};
Print "$key => $value \ n";
}

Iv. common usage of Perl hash

1.exists function

To see if a key exists in the hash, you can use the EXISTS function, which returns true if the key exists in the hash, regardless of whether there is a corresponding value.

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3, "D" =>4);
If (exists $hash {' A '})
{
Print "true";
}

2.delete function

The delete function deletes a given key, including its corresponding value, from the hash. If this key is not present, there is nothing to do without warning or error messages.

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3, "D" =>4);
Delete $hash {' A '};
foreach $key (sort keys%hash)
{
$value = $hash {$key};
Print "$key => $value \ n";
}

Interpolation of 3.Perl Hash elements

You can use a single hash element in a double quote string, but it does not support the entire hash interpolation.

Copy Code code as follows:

My%hash = ("A" =>1, "B" =>2, "C" =>3, "D" =>4);
foreach $key (sort keys%hash)
{
Print "$key => $hash {$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.