Perl hash
A hash is a collection of key/value pairs.
Hash variables in Perl start with a percent sign (%).
Access hash element format: ${key}.
The following is a simple hash example:
#!/usr/bin/perl %data = ('google', 'google.com', 'php', 'php.cn', 'taobao', 'taobao.com'); print "$data{'google'} = $data{'google'}\n"; print "$data{'php'} = $data{'php'}\n"; print "$data{'taobao'} = $data{'taobao'}\n";
Execute the above program, the output result is:
Create hash Hash
You can create a hash in the following two ways:
1. Set the value
$data{'google'} = 'google.com'; $data{'php'} = 'php.cn'; $data{'taobao'} = 'taobao.com';
for each key. 2. Set the
list through a list. The first element in is key and the second is value.
%data = ('google', 'google.com', 'php', 'php.cn', 'taobao', 'taobao.com');
You can also use the => symbol to set key/value:
%data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com');
The following example is a variant of the above example, use - Instead of quotation marks:
%data = (-google=>'google.com', -php=>'php.cn', -taobao=>'taobao.com');
Using this method, spaces cannot appear in the key. The way to read elements is:
$val = %data{-google} $val = %data{-php}
Access hash elements
Access hash element format:${key}, the example is as follows:
#!/usr/bin/perl %data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com'); print "$data{'google'} = $data{'google'}\n"; print "$data{'php'} = $data{'php'}\n"; print "$data{'taobao'} = $data{'taobao'}\n";
Execute the above program, the output result is:
Read the hash Value
You can extract values from a hash just like an array.
Extract hash value to array syntax format: @{key1,key2}.
#!/uer/bin/perl %data = (-taobao => 45, -google => 30, -php => 40); @array = @data{-taobao, -php}; print "Array : @array\n";
Execute the above program, the output result is:
Array : 45 40
Read the hash key and value
Read all keys
us You can use the keys function to read all keys of a hash. The syntax format is as follows:
keys %HASH
This function returns an array of all keys of all hashes.
#!/usr/bin/perl %data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com'); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n";
Execute the above program, the output result is:
taobao google php
Similarly, we can use the values function to read all the values of the hash, the syntax format is as follows:
values %HASH
This function returns an array of all values of all hashes.
#!/usr/bin/perl %data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com'); @urls = values %data; print "$urls[0]\n"; print "$urls[1]\n"; print "$urls[2]\n";
Execute the above program, the output result is:
taobao.com php.cn google.com
Detect whether the element exists
If you read a non-existent key/value pair in the hash , will return the undefined value, and there will be a warning when executing.
In order to avoid this situation, we can use the exists function to determine whether the key exists. When it exists, read:
#!/usr/bin/perl %data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com'); if( exists($data{'facebook'} ) ){ print "facebook 的网址为 $data{'facebook'} \n"; } else { print "facebook 键不存在\n"; }
Execute the above program, and the output result is:
facebook 键不存
In the above code, we used the IF...ELSE statement, which we will introduce in detail in the following chapters.
Get the hash size
The hash size is the number of elements. We can get the hash by first getting all the element arrays of key or value, and then calculating the number of array elements. The size of This is done via simple assignment. But to delete hash elements you need to use the
deletefunction:
#!/usr/bin/perl %data = ('google'=>'google.com', 'php'=>'php.cn', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - 哈希大小: $size\n"; @values = values %data; $size = @values; print "2 - 哈希大小: $size\n";
Execute the above program, the output result is: