% H = (‘a’, 1, ‘b’, 2); a is key 1 is value b is key 2 is value
Starts with%, () is an empty hash
% H = (‘a’ => 1, ’b’ => 2); more obviously, you can also omit the quotes, but the default is all strings. When there are numbers, it is not your original intention
Element form: $ h {‘a’} #Note the use of $
The element is a simple variable, use $. % h {2.5}% {-1} $ {$ ee} are all possible decimals negative numbers simple variables can all be keys
Assignment of associative arrays
1, Standard:% a = (‘a’, 1, ‘b’, 2);
2. Assignment between array and hash:% [email protected] The hash is also an array, but there is no order
3, @ a =% h The order of the array elements is random
4, ($ a,% h) [email protected] #Not very likely to put the first value of the array into the simple variable $ a and then put the rest into the hash% h, so @array The number must be odd
5,% h = (% first,% second) #Waste memory
6.% h1 = (% h2, ‘k’, ‘v’) #Add a set of key values, which must appear in pairs
7. The return of a function, such as the result of split (), can also be assigned to a hash:
% H = split (/, /, ‘a, b, c, d’)
8. Snippet: @key = (‘a’, ’b’, ‘c’)
H @hash {@keys} [email protected] {reverse (@keys)}
注意 # Note that @ cannot be changed to $, because the key in $ hash {} is a simple variable, so a number is in parentheses.
现在 Now I want to circle an array, so it needs to be @ before, and @ is inside the brackets.
Should correspond back and forth
If you enter $ hash {@keys} because the key in $ hash braces is a simple variable, this is equivalent to $ hash {@keys length}, which is @hash {3}
Third, element assignment and operation
1, increase: $ h {‘a’} = 1 is created if it does not exist, it is also created if the hash does not exist
2. Delete: delete $ h {‘a’}
3. Do not use order related functions such as push pop shift splice, otherwise the result is unknown
4. Display: Cannot print "% hash"; This only outputs a string:% hash
If print% hash is used, no spaces are displayed
Use traversal method to display one by one
5, $ a =% hash $ a is assigned the value x x is the number of existing elements / number of allocated memory
Fourth, associative array operation functions
Key (% h) or key% h #Return the list of keys
Value (% h) or value% h #Return value list
Each (% h) loop: ($ key, $ value) = each (% hash)
不要 Do not add or delete elements in the loop, because the hash is out of order. The end of the loop returns undef
Exist: See if the keyword exists
Exist $ h {‘key’}
Undef (% h): delete hash equivalent to% = ()
Delete
Introduction to Perl language--5--hashing and hashing
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.