Perl Learning 5 Hash, perllearninghash

Source: Internet
Author: User
Tags perl operator

Perl Learning 5 Hash, perllearninghash

[This article is original. Do not reprint it without permission]

Hash is a data structure. It is similar to an array in that it can accommodate any number of values and can be used as needed. The difference between hash and array lies in the indexing method, arrays are indexed by numbers, while hash is indexed by names. That is to say, the hash index value, which is called a key, is not a number, but any unique string. But it must also be a unique string.

We can also think of the hash as a big bucket, where each data has associated tags. You can reach out to the bucket and retrieve a tag to see what data is attached to it. However, the bucket does not have the so-called "first" element, and there is only a pile of data. Is a set of key-value pairs.

 

To access the hash element, use the following syntax:


This is similar to accessing arrays. It only uses curly brackets instead of square brackets to represent the index value (hash key). When you access a value that does not exist in the hash table, undef is obtained.



To refer to the entire hash, you can use the percent sign (%) as the prefix. Hash can be converted into a list, and vice versa. Assigning a hash value is equivalent to assigning a value in the context of the list. The elements in the list should be key-value pairs. You can change a hash table to a key-Value Pair list. Of course, the obtained key-value pairs are not necessarily displayed in the order of original values. Therefore, when hash is used, either the storage order of elements is irrelevant or the sorting can be easily performed during element output.

 

Hash Value assignment:


This expands % any_hash into a key-value Pair list, which looks like (key, value, key, value. Then, a new list (value, key, value, key,...) is formed by using the reverse list flip function, and key values are exchanged.

When assigning a list to a hash, it is often found that the key-value pairs in the list are not easy to distinguish. So we introduced the fat arrow =>


To add more information, make sure that each line has a set of key-value pairs and a comma at the end. At the same time, the above Code can be abbreviated:


Of course, not all cases can do this, because the hash key can be a string of any form, so if the content of a key looks like a Perl operator, there will be problems. There is also a common option to omit the key name quotation marks: to retrieve the elements of a specific key name in curly brackets.

Hash function: the keys function can return the hash key list, while the values function can return the corresponding value list.


Result:

The abc sequence is different, but the returned Key List and value list are in the same order. In a scalar context, both functions return the number of elements (key-value pairs) in the hash. This computing process does not need to traverse the entire hash, so it is very efficient.

 

If you need to iterate (process each element one by one) the entire hash, the common syntax is to use the each function, which can include a list of two elements to return the key-value pair.

Result:

When Perl executes each % hash but does not have any key-value pairs, each returns an empty list.

If you need to process the hash in sequence, you only need to sort the keys:


Result:

To check whether a key exists in the hash, you can use the exists function. It returns true or false, indicating whether the key exists or not, regardless of the corresponding value of the key.

The Delete function can Delete the specified key and corresponding value from the hash. Without such a key, it will end directly without any warning or error messages.


This is not the same as "Saving undef into Hash elements. In both cases, exists ($ book {"betty"}) returns the opposite result. After the delete operation, the key will not appear in the hash. However, after undef is saved, the key will certainly exist.

 

Since a Perl program runs in an environment, it must be aware of the surrounding environment. Perl accesses this information by accessing the % ENV hash.


Result:


Exercise:


Question 1:

Result:

If you want to use my to declare the hash, you must assign values to the elements after the declaration. The My operator intelligently declares independent variables and cannot be used to declare elements in an array or hash.


Question 2:

Result:

Additional:

Result:

The first foreach loop processes each word one by one. This loop contains the most important line in the entire program. It will add 1 to the value of $ count {$ word} and save it back to $ count {$ word }.

 

Question 3:


The result is as follows:


This topic improves my understanding and usage of Hash tables. The usage of each $ and % will be well analyzed in subsequent Perl programming. I feel very young.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.