Perl Hash
A hash iskey/valuea collection of pairs.
In Perl, hash variables start with a percent sign (%).
Format for accessing hash elements:${key}。
The following is a simple hash example:
Example
Executing the above program produces the following output:

Creating a Hash
A hash can be created in the following two ways:
1. Set value for each key
$data{'google'} = 'google.com';
$data{'example'} = 'example.com';
$data{'taobao'} = 'taobao.com';
2. Set via a list
In the list, the first element is the key and the second is the value.
%data = ('google', 'google.com', 'example', 'example.com', 'taobao', 'taobao.com');
You can also use=>the symbol to set key/value:
%data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com');
The following example is a variant of the above, using-to replace quotes:
%data = (-google=>'google.com', -example=>'example.com', -taobao=>'taobao.com');
Using this method, keys cannot contain spaces. The way to read elements is:
$val = $data{-google}
$val = $data{-example}
Accessing Hash Elements
Format for accessing hash elements:${key}, the example is as follows:
Example
Executing the above program produces the following output:

Reading Hash Values
You can extract values from a hash just like from an array.
Syntax format for extracting hash values into an array:@{key1,key2}。
Example
Executing the above program produces the following output:
Array : 45 40
Reading Hash Keys and Values
Reading All Keys
We can use thekeysfunction to read all keys of the hash. The syntax format is as follows:
keys %HASH
This function returns an array of all keys of the hash.
Example
Executing the above program produces the following output:
taobao google example
Similarly, we can use thevaluesfunction to read all values of the hash. The syntax format is as follows:
values %HASH
This function returns an array of all values of the hash.
Example
Executing the above program produces the following output:
taobao.com example.com google.com
Checking if an Element Exists
If you read a non-existent key/value pair in a hash, it returns aundefinedvalue, and a warning will be generated during execution.
To avoid this situation, we can use theexistsfunction to check whether the key exists, and read it when it exists:
Example
Executing the above program produces the following output:
facebook 键不存在
In the above code we used theIF...ELSEstatement, which will be introduced in detail in later chapters.
Getting the Hash Size
The hash size is the number of elements. We can get the hash size by first obtaining an array of all keys or values, and then counting the number of array elements. An example is as follows:
Example
Executing the above program produces the following output:
1 - 哈希大小: 3 2 - 哈希大小: 3
Adding or Deleting Elements in a Hash
Adding key/value pairs can be done with simple assignment. However, to delete hash elements you need to use thedeletefunction:
Example
Executing the above program produces the following output:
1 - 哈希大小: 3 2 - 哈希大小: 4 3 - 哈希大小: 3
Iterating Over a Hash
We can use foreach and while to iterate over a hash:
Example - Using foreach
Example - Using while
Executing the above program produces the following output:
example.com google.com taobao.comOther Extensions