In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what the Perl hash table is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Perl basic Perl hash table
What is a Perl hash table
A Perl hash table is a data structure similar to an array in which values can be stored or retrieved. However, unlike an array, the index is not a number, but a name. That is, the index (in this case, we call it key) is not a number but an arbitrary * string.
Key can be any string, you can use any string as key, but they are *.
Another way to think about hash is to think of it as an abarrelofdata, each with a corresponding label. The element corresponding to this tag can be accessed through the tag. But there is no concept of "* *" elements. In the array, the array elements are numbered starting with 0 minute 1, 1, and 2. But in hash, there is no definite order, so there are no * elements. It's just a collection of key/value pairs.
Both keys and values are arbitrary scalars, but keys is usually converted to a string. Therefore, if the expression 50 amp 20 is used as the keys, it is usually converted to the 3-character string "2.5".
Because of Perl's "no unnecessary restrictions" design philosophy: hash can be any size, from empty hash (no key/value pairs) to any amount of memory you allow.
Keys is * *, but values can be repeated. The value of hash can be a number, a string, a undef, or a mixture of them, but the key is *.
Why use Perl hash table
Think of hash as a simple database in which there can be a piece of data under each key. If your task is about "query repetition", "* *", "cross-reference", "query table", then hash is likely to help you in such applications.
II. Access of Perl hash table elements
To access the hash element, use the following syntax: $hash {$some_key}
This is similar to accessing array elements, where curly braces ({}) are used on the subscript (key) instead of square brackets ([]). Now the expression of key is a string, not a number.
The name of hash has the same naming convention as other identifiers in Perl (letters, numbers, underscores, but cannot start with numbers).
Visit a hash element that does not exist and you will get undef.
1. 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 table assignment
You can copy between hash using the following syntax:
% new_hash=%old_hash
It is more common to convert hash into other forms. For example, we can reverse 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 which are values, so the big arrow symbol (= >) is invented. When you need a comma, you can replace it with a big arrow symbol.
Such as:
My%last_name= (
"www" = > 1
"eee" = > 2
);
3. Perl hash table function
Some useful functions can operate on the entire hash.
1.keys and values functions
The keys function returns all keys,values functions of this hash will return all values. If there are no elements in the hash, this function returns an empty list.
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3)
Printmy@k=keys%hash
Printmy@v=values%hash
2.each function
If you want to iterate over each element of hash, a common way is to use the each function, which returns a list of two elements corresponding to key/value.
When iterating over the same hash function, the next key/value pair is returned until all elements are accessed. If there are no more key/value pairs, the each function returns an empty table.
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3)
While (($key,$value) = each%hash)
{
Print "$key= > $value\ n"
}
Of course, the key/vlaue pairs returned by each are out of order (it is in the same order as the keys and values functions return). If you want to discharge them in order, you can sort them (using sort).
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3, "d" = > 4)
Foreach$key (sortkeys%hash)
{
$value=$hash {$key}
Print "$key= > $value\ n"
}
Fourth, the common usage of Perl hash table
1.exists function
To see if a key exists in hash, you can use the exists function. If the key exists in hash, true is returned, regardless of whether there is a corresponding value.
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3, "d" = > 4)
If (exists$hash {'a'})
{
Print "true"
}
2.delete function
The delete function removes a given key (including its corresponding value) from the hash. If the key does not exist, nothing will be done and there will be no warning or error message.
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3, "d" = > 4)
Delete$hash {'a'}
Foreach$key (sortkeys%hash)
{
$value=$hash {$key}
Print "$key= > $value\ n"
}
Interpolation of 3.Perl hash table elements
You can use a single hash element in a double-quoted string, but it does not support the interpolation of the entire hash.
My%hash= ("a" = > 1, "b" = > 2, "c" = > 3, "d" = > 4)
Foreach$key (sortkeys%hash)
{
Print "$key= > $hash {$key}\ n"
}
The above is all the contents of the article "what is the Perl hash table?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.