HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Hash is akey / value pairs.
Perl hash variables start with a percent sign (%) marks.
Access hash elementformat: $ {key}.
The following is a simple hash instance:
#!/usr/bin/perl %data = ('google', 'google.com', 'w3resource', 'w3resource.net', 'taobao', 'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'w3resource'} = $data{'w3resource'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";
The above program, the output is:
You can create a hash of the following two ways:
$data{'google'} = 'google.com'; $data{'w3resource'} = 'w3resource.net'; $data{'taobao'} = 'taobao.com';
The first element in the list of key, the second is value.
%data = ('google', 'google.com', 'w3resource', 'w3resource.net', 'taobao', 'taobao.com');
You can also use the=> symbol to set the key / value:
%data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com');
The following examples are variants of the above examples, use- instead of quotation marks:
%data = (-google=>'google.com', -w3resource=>'w3resource.net', -taobao=>'taobao.com');
Using this mode key can not have spaces, read element way:
$val = %data{-google} $val = %data{-w3resource}
Access hash elementformat: $ {key}, examples are as follows:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'w3resource'} = $data{'w3resource'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";
The above program, the output is:
You can like an array extract value from the hash.
Hash value extracting the arraysyntax: @ {key1, key2}.
#!/uer/bin/perl %data = (-taobao => 45, -google => 30, -w3resource => 40); @array = @data{-taobao, -w3resource}; print "Array : @array\n";
The above program, the output is:
Array : 45 40
We can use the functionkeys to read all the hash keys, syntax is as follows:
keys %HASH
This function returns an array of all the key hash of all.
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com'); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n";
The above program, the output is:
taobao google w3resource
What can I use a similar function to read all thevalues of the hash values, syntax is as follows:
values %HASH
This function returns an array of all value of all hashes.
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com'); @urls = values %data; print "$urls[0]\n"; print "$urls[1]\n"; print "$urls[2]\n";
The above program, the output is:
taobao.com w3resource.net google.com
If you read a non-existent key / value pair in the hash, and returnsundefined value, and in the implementation of a warning reminder.
To avoid this situation, we can use theexists function to determine whether the key exists, when existence reads:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com'); if( exists($data{'facebook'} ) ){ print "facebook 的网址为 $data{'facebook'} \n"; } else { print "facebook 键不存在\n"; }
The above program, the output is:
facebook 键不存
The above code we useIF /en. ELSE statement in a later section we'll introduce specific.
Hash size is the number of elements that we can get through all the elements of the array to the key or value, and then calculate the number of array elements to obtain the size of the hash, examples are as follows:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - 哈希大小: $size\n"; @values = values %data; $size = @values; print "2 - 哈希大小: $size\n";
The above program, the output is:
1 - 哈希大小: 3 2 - 哈希大小: 3
Add key / value pairs can be done by simple assignment. However, removing hash elements you need to use thedelete function:
#!/usr/bin/perl %data = ('google'=>'google.com', 'w3resource'=>'w3resource.net', '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 program, the output is:
1 - 哈希大小: 3 2 - 哈希大小: 4 3 - 哈希大小: 3