Perl Learning 5 Hash

Source: Internet
Author: User

"Original, do not reprint without permission"

A hash is a data structure that is similar to and to a group in that it can hold any number of values and can be used on demand, and that the array is indexed by numbers, and the hashes are indexed by names. That is, the index value of the hash, which is called the key, is not a number, but any unique string. But it must also be a unique string.

We can also think of the hash like this, and try to think of it as a big bucket of data, where each data has an associated label. You can reach out to the bucket and take out a label to see what the data is attached to it. But there is no "first" element in the bucket, only a bunch of data. is a collection of key-value pairs.

To access the hash element, you need to use the following syntax:


This is similar to accessing an array, with the use of curly braces instead of square brackets to represent index values (hash keys), and access to values that do not exist in the hash table are undef.



To refer to the entire hash, you can prefix it with a percent sign (%) . Hashes can be turned into lists, and vice versa. The Hashi value is equivalent to assigning a value in the context of a list, and the elements in the list should be key - value pairs. You can turn a hash table into a List of key-value pairs. Of course, the resulting key - value pairs do not necessarily expand in the order in which they were originally assigned. So choosing where to use the hash, either the element storage order is irrelevant or it can be easily sorted when the element is output.

Hashi Value:


This expands the%any_hash into a List of key-value pairs, which appears to be (key,value,key,value,...) this way. Then use reverse 's list rollover function to form one (Value,key,value,key,...) Such a new list, the key values are agreed to interchange.

When assigning a list to a hash, it is often found that key - value pairs in the list are not easily distinguishable. So the introduction of the fat arrow =


When you need to add more information, just make sure that each row has a set of key- value pairs and a comma at the end of the line. At the same time the above code can be abbreviated as:


Of course, not all cases can be done, because the hash key can be any form of string, so if the content of a key appears to be a Perl operator, there will be a problem. There is also a common place to allow omitting key-name quotes: The element that retrieves a specific key name in curly braces.

hash function: Thekeys function returns a list of the keys of the hash, and The values function returns a list of the corresponding value.


Results:

The order of the ABC is different, but the order of the returned key list and value list is consistent. In a scalar context, both functions will return the number of hash elements (key - value pairs). This calculation process does not have to traverse the entire hash, so it is very efficient.

If you need to iterate over the entire hash of each of these elements, it is common to use the each function to return a key - value pair in the form of a list of two elements .

Results:

When Perl executes each%hash and There are no key - value pairs, each returns an empty list.

If you need to sequentially process the hash, simply sort the key:


Results:

To check if a key exists in the hash, you can use the Exists function, which returns TRUE or false, indicating whether the key exists or not, regardless of the value corresponding to the key.

The delete function removes the specified key from the hash and corresponds to the value. Without such a key, it would end directly without any warning or error message.


This is not the same as "depositing undef into a hash element". In both cases,theexists ($book {"Betty"}) will produce the opposite result. after the delete , the key does not appear in the hash, but after depositing the undef , the key will certainly exist.

Since the Perl program is running in an environment, it needs to be aware of the surrounding environment. the way Perl accesses this information is to access the %env Hash.


Results:


Exercises:


First question:

Results:

If you intend to use my to declare a hash, you must declare it before you can assign a value to the element. the My operator intelligently declares a separate variable and cannot be used to declare an array or an hash element.


The second question:

Results:

Additional:

Results:

The first foreach loop handles each word one item at a. The loop contains the most important line of the entire program, which adds 1to the value of the $count {$word} and then saves it back to $count {$word} .

Question three:


The results are as follows:


This chapter has deepened my understanding and use of the hash table. the usage of each $ and % is analyzed well in subsequent Perl programming . I feel very young.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Perl Learning 5 Hash

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.