Hash
A hash is a data structure that is similar to an array in that it can tolerate any number of values and can be used on demand, whereas he and the group differ in the way the indexes are indexed and the arrays are indexed by numbers and the hashes are indexed by names.
The hash key is unique, and the hash value can be duplicated.
Application Scenarios for hashing
Example:
- Find a surname by name;
- Find the IP address by hostname or the host name by IP address;
- Count the number of occurrences according to the word;
- Count the number of disk blocks used by each person by user name;
- Find your name according to your driver's license number.
Accessing hash elements
For example: $hash {$some _key}//Note using curly braces {} is not in brackets
Assignment: $family _name{' Fred '}= ' Flintstone ';
$family _name{' Barney '}= ' rubble ';
Access the entire hash
%some_hash= (' foo ', +, ' bar ', 12.4); %< Hash name >= (Key,value,key,value ... )
You can use the following code to easily view:
My @k=keys%some_hash;//keys after the explanation
foreach (@k)
{
Print "$_=> $some _hash{$_}". \ n ";
}
Note that the output order may be disorderly.
@tmp_hash =%some_hash==> results: Tmp=foo 12.4
Hashi value
My%new_hash=%old_hash;==> allocates memory for New_hash, and then assigns the value of Old_hash to the past. is not a simple reference
My%inverse_hash=reverse%any_hash;
The above comparison applies to: Find the IP address by hostname or the host name by IP address
fat arrow = =
The main purpose is to improve the following pattern:%some_hash= (' foo ', ' + ', ' bar ', 12.4);
such as: My%some_hash= (
' Foo ' =>35,
' Bar ' =>12.4,
' Dnio ' =>undef,
);
Note: There is a comma at the end of each line.
hash Function
Keys values
The Keys function returns a list of the key hashes that the values function can return.
such as: My%hash= (' A ' =>1, ' B ' =>2, ' C ' =>3,);
My @k=keys%hash;//will get a B C
My @v=values%hash;//will get 1 2 3
If you use: my $count =keys%hash;//will get 3 that means the hash has 3 pairs of keys
Each function
If you need to iterate over the entire hash, the common use is to use each function. The key and value pairs of the hash table are returned each time. Until all the elements have been accessed.
Example: while (($k, $v) =each%hash) {
print "$k = $v \ n";
}
Another example: the order of each return key-value pair is disorderly, but it is the same order that keys and values return, which is the natural order of the hash. If you need to process the hash sequentially, you only need to sort the keys.
foreach $key (sort keys%hash) {
$value = $hash {$key};
Say "$key = $value";
}
exists function
Checks if a key exists in the hash table.
If (exists $hash {' foo '}) {
Say "There is a Foo";
}
Delete function
Removes the specified key and the corresponding value from the hash. (Always successful)
My $person = "Betty";
Delete $hash {$person};
%env Hash
Since the Perl program is running in an environment, it needs to be aware of the surrounding environment. The way Perl accesses these environment information is to access the%env hash. Such as:
Print "path is $ENV {path}";
Perl Learning notes--hashing