Perl notes 6 hash

Source: Internet
Author: User

1. the hash key is unique and the value can be repeated!

2. Access the hash Element

$ Hashname {"$ key"}; # hash is braces, array is square brackets, and key is string

$ Family_name {"Fred"} = "firstd"; # assign a value to the hash Element

3. the hash key supports any expression.

$ Foo = "Na ";

$ Family_name {$ Foo. "Me"}; # obtain the value of $ family_name {"name "}.

4. Access the entire hash

% Family_name

5. Hash and list Conversion

% Some_hash = {"peison1", 35, "person2", 43, "person3", 56}; convert the list to a hash

@ Arry = % some_hash; Convert hash to list

6. Hash sorts key-value pairs again, and the output and input order may be different.

7. Hash replication and Inversion

% New_hash = % old_hash; first convert old_hash to list, then convert the list to new_hash

% In_hash = reverse % old_hash; key-value swaps are generally performed only when the hash value is unique. Otherwise, the later values of the same key will overwrite the original values.

8. Fat arrows

As another method of writing comma, it is easy to distinguish key-value pairs

% My_hash = (

"Fred" => "fsas ",

"Fdsred" => "fsdsas ",

"Frsdesdd" => "fdssas ",

)

The end of the comma cannot be omitted, and parentheses are used on both sides.

9. Get the key value

@ Key = keys % hash; # Return key list

@ Value = values % hash; # Return Value List, consistent with the key order

$ Num = keys % hash; # Number of Return key-value pairs

10. List hash key-value pairs

While ($ key, $ value) = each % hash) {print "$ key => $ value \ n" ;}# the returned result of each is a list, the result of boolean evaluation in while is the number of elements in the list (2)

11. Common Hash Functions

Exists $ hash {"DSA"}; # determines whether a DSA key exists.

Delete $ hash {"DSA"}; # No error is returned regardless of whether the specified key exists or not. It is different from assigning a value to UNDEF.

12. You can insert a single hash value into a double quotation mark string. "hash value is $ hash {$ person}" does not support interpolation of the entire hash.

13. Get environment variable configuration and other information

You can access % env to obtain the PATH value, for example, print "path is $ ENV {path }".

Get the environment variable set outside the program: $ ENV {character}


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.