1. What is Perl Hash?
A hash is a data structure. Similar to an array, a hash can store a value or retrieve the value from it. However, unlike an array, its index is not a number but a name. That is to say, the index (here we call it key) is not a number but an arbitrary and unique string.
Keys can be any string. You can use any string as the key, but they are unique.
Another way to think about hash is to think of it as a bunch of data (a barrel of data), and each data has a corresponding tag. You can use tags to access the elements corresponding to this tag. However, there is no concept of the "first" element. In the array, the array elements start from 0, Perl learning hash _ Ma Liang's computer technology 1, 2. However, there is no definite order in hash, so there is no first element. Only a set of key/value pairs.
Keys and values are arbitrary scalar values, but keys are usually converted to strings. Therefore, if expression 50/20 is used as the keys, it is usually converted to a 3-character string "2.5 ".
Because Perl's design philosophy of "no unnecessary restrictions": hash can be any size, from empty hash (no key/value Pair) to any size that your memory allows.
Keys are unique, but values can be repeated. Hash values can be numbers, strings, undef, or their mixture, but keys are unique.
Why use Perl Hash
We can regard hash as a simple database, where each key can have a piece of data. If your task is about: "Duplicate queries", "unique", "cross-referenced", "query table ", then hash is likely to help Perl learn the hash _ Ma Liang's computer technology in such applications.
Ii. Access to Perl Hash Elements
To access the hash element, you can use the following syntax: $ hash {$ some_key}
This is similar to accessing array elements. Here, the subscript (key) uses curly brackets ({}) instead of square brackets ([]). The key expression is a string instead of a number.
The hash name is the same as the naming rules for other identifiers in Perl (consisting of letters, numbers, and underscores, but cannot start with a number ).
Access the nonexistent hash element to obtain undef.
1. Perl Hash as a whole
To reference the entire hash, use the percent sign (%) as the prefix.
For convenience, hash can be converted to a list or vice versa.
2. Perl Hash assignment
You can use the following syntax to copy between hash:
% New_hash = % old_hash;
Converting hash into other forms is more common. For example, we can reverse the hash:
% Inverse_hash = reverse % any_hash;
3. Large arrow symbol (=>)
When assigning values to hash, it is sometimes not obvious which elements are keys and those are values, so the big arrow symbol (=>) is invented ). When you need a comma, you can use the arrow symbol to replace them.
For example:
Copy codeThe Code is as follows: my % last_name = (
"Www" => 1,
"Eee" => 2,
);
Iii. Perl Hash Functions
Some useful functions can operate the entire hash.
1. keys and values Functions
The keys function returns all the keys of the hash, and the values function returns all the values. If there are no elements in the hash, this function returns an empty list.
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3 );
Print my @ k = keys % hash;
Print my @ v = values % hash;
2. each Function
To iterate each element of hash, a common method is to use the each function, which returns the list of two elements corresponding to the key/value.
When an iteration is performed on the same hash function, the next key/value pair is returned until all elements are accessed. If no more key/value pairs exist, the each function returns an empty table.
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3 );
While ($ key, $ value) = each % hash)
{
Print "$ key => $ value \ n ";
}
Of course, the order of key/vlaue pairs returned by each is chaotic (the order is the same as that returned by the keys and values functions ). If you want to discharge them in order, you can sort them (using sort ).
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3, "d" => 4 );
Foreach $ key (sort keys % hash)
{
$ Value = $ hash {$ key };
Print "$ key => $ value \ n ";
}
Iv. Common usage of Perl Hash
1. exists Function
To check whether a key exists in the hash, you can use the exists function. If the key exists in the hash, true is returned, regardless of whether the corresponding value exists.
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3, "d" => 4 );
If (exists $ hash {'A '})
{
Print "true ";
}
2. delete function
The delete function deletes a given key (including its corresponding value) from the hash. If this key does not exist, nothing is done and no warning or error message is provided.
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3, "d" => 4 );
Delete $ hash {'A '};
Foreach $ key (sort keys % hash)
{
$ Value = $ hash {$ key };
Print "$ key => $ value \ n ";
}
3. Perl Hash element interpolation
You can use a single hash element in a double quotation mark string, but it does not support interpolation of the entire hash.
Copy codeThe Code is as follows: my % hash = ("a" => 1, "B" => 2, "c" => 3, "d" => 4 );
Foreach $ key (sort keys % hash)
{
Print "$ key => $ hash {$ key} \ n ";
}