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