The Camel spirit in Perl

Source: Internet
Author: User

The hash data structure in Perl.

A hash is a data structure that consists of a pair of keys-values pairs.

These keys and values are arbitrary scalars, but the keys are always converted to strings and must be unique strings.

Access to the hash element, $hash {$some _key} # #和访问数组的做法类似, just using curly braces {} instead of square brackets [].

Access to the hash table that does not exist is worth to undef.

$family _name{' barney '} = ' rubble ' # #完成对某一哈希元素的单一赋值

Accesses the entire hash,%family_name # #此时哈希被转换成列表, and the elements in the list are key-value pairs.

%family_name = (' foo ', ' n ', ' bar ', 12.4);

My%new_hash =%old_hash # #整个hash的赋值, the hash is decomposed into key-value pairs first,

After you merge the key-value pairs into a hash. So the back and forth order can change.

You can also write the hash as follows to assign a value to my%last_name = (

' Fred ' = ' Flintstone ',

' Dino ' = undef

);

= = is called a fat arrow to explicitly indicate the key value pair where the key ' ' can be omitted. $score {' Fred '} can also be shortened to $score{fred} directly.

The hash function.

Keys and values return the key list and value list in the hash, respectively.      My @k = Keys%hash; My @v=values%hash;

In a scalar context, returns the number of key elements and value elements, respectively.

if (%hash) {print "....";} # #在标量上下文, as long as there is at least one key value pair in the hash, it returns true.

Each function returns a set of key values from hash.

while (($key, $value) = each%hash) {print "....";} # #在while中时标量上下文, returns 2 or 0.

exists function to check if a key exists in the hash.

If (exists $book {"Dino"}) {print "...";}

The delete function deletes a key value in the hash, not the assignment undef, but rather deletes it directly.

Delete $books {$person};

It is not possible to interpolate the entire hash directly within double quotes.

foreach $persons (sort keys $books)

if ($books ($persons)) {print "$person has $books {persons}";} # #只可以这样完成单个哈希的内插

The Camel spirit in Perl

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.