Perl hash
The hash is a collection of key/value pairs.
The hash variable in Perl is in a percent sign (%) The tag starts.
Access hash element format: $.key.
Here's a simple hash example:
#!/usr/bin/perl %data = ('google', 'google.com', 'w3cschool', 'w3cschool.cn', 'taobao', 'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'w3cschool'} = $data{'w3cschool'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";
The above procedure is performed and the output is:
Create a hash
There are two ways to create a hash:
First, set value for each key
$data{'google'} = 'google.com'; $data{'w3cschool'} = 'w3cschool.cn'; $data{'taobao'} = 'taobao.com';
Second, through the list settings
The first element in the list is key and the second is value.
%data = ('google', 'google.com', 'w3cschool', 'w3cschool.cn', 'taobao', 'taobao.com');
You can also set key/value using the symbol:
%data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com');
The following example is a variant of the above example, - of quotation marks:
%data = (-google=>'google.com', -w3cschool=>'w3cschool.cn', -taobao=>'taobao.com');
In this way key cannot appear spaces and the element is read as:
$val = %data{-google}
$val = %data{-w3cschool}
Access hash elements
Access to hash element format: $'key' as follows:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'w3cschool'} = $data{'w3cschool'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";
The above procedure is performed and the output is:
Read the hash
You can extract values from hash like an array.
The hash value is extracted into the array syntax format: s key1, key2.
#!/uer/bin/perl %data = (-taobao => 45, -google => 30, -w3cschool => 40); @array = @data{-taobao, -w3cschool}; print "Array : @array\n";
The above procedure is performed and the output is:
Array : 45 40
Read the key and value of the hash
Read all keys
We can use the keys function to read all the keys of the hash, in the following syntax format:
keys %HASH
The function returns an array of all keys for all hashes.
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n";
The above procedure is performed and the output is:
taobao google w3cschool
Similarly, I can use the values function to read all the values of the hash, in the following syntax format:
values %HASH
The function returns an array of all values for all hashings.
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); @urls = values %data; print "$urls[0]\n"; print "$urls[1]\n"; print "$urls[2]\n";
The above procedure is performed and the output is:
taobao.com w3cschool.cn google.com
Detect the presence of an element
If you read a key/value pair that does not exist in the hash, the underfined value is returned and a warning alert is issued when executed.
To avoid this, we can use the exists function to determine whether key exists and reads when it exists:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); if( exists($data{'facebook'} ) ){ print "facebook 的网址为 $data{'facebook'} \n"; } else { print "facebook 键不存在\n"; }
The above procedure is performed and the output is:
facebook 键不存
We used IF... in the code above. ELSE statements, which we'll cover in a later section.
Gets the hash size
The hash size is the number of elements, and we can get the size of the hash by first getting an array of all elements of key or value, and then calculating how many elements of the array are, as follows:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - 哈希大小: $size\n"; @values = values %data; $size = @values; print "2 - 哈希大小: $size\n";
The above procedure is performed and the output is:
1 - 哈希大小: 3 2 - 哈希大小: 3
Hash adds or removes elements
Adding key/value pairs can be done with a simple assignment. But to remove the hash element you need to use the delete function:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3cschool'=>'w3cschool.cn', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - 哈希大小: $size\n"; # 添加元素 $data{'facebook'} = 'facebook.com'; @keys = keys %data; $size = @keys; print "2 - 哈希大小: $size\n"; # 删除哈希中的元素 delete $data{'taobao'}; @keys = keys %data; $size = @keys; print "3 - 哈希大小: $size\n";
The above procedure is performed and the output is:
1 - 哈希大小: 3 2 - 哈希大小: 4 3 - 哈希大小: 3