Latest web development tutorials

Perl hashes

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', 'w3big', 'w3big.com', 'taobao', 'taobao.com');

print "\$data{'google'} = $data{'google'}\n";
print "\$data{'w3big'} = $data{'w3big'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";

The above program, the output is:


Creating a hash

You can create a hash of the following two ways:

First, set the value for each key

$data{'google'} = 'google.com';
$data{'w3big'} = 'w3big.com';
$data{'taobao'} = 'taobao.com';

Second, through the set list

The first element in the list of key, the second is value.

%data = ('google', 'google.com', 'w3big', 'w3big.com', 'taobao', 'taobao.com');

You can also use the=> symbol to set the key / value:

%data = ('google'=>'google.com', 'w3big'=>'w3big.com', 'taobao'=>'taobao.com');

The following examples are variants of the above examples, use- instead of quotation marks:

%data = (-google=>'google.com', -w3big=>'w3big.com', -taobao=>'taobao.com');

Using this mode key can not have spaces, read element way:

$val = %data{-google}
$val = %data{-w3big}

Access hash element

Access hash elementformat: $ {key}, examples are as follows:

#!/usr/bin/perl

%data = ('google'=>'google.com', 'w3big'=>'w3big.com', 'taobao'=>'taobao.com');

print "\$data{'google'} = $data{'google'}\n";
print "\$data{'w3big'} = $data{'w3big'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";

The above program, the output is:


Read hash value

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, -w3big => 40);

@array = @data{-taobao, -w3big};

print "Array : @array\n";

The above program, the output is:

Array : 45 40

Reads the hash key and value

Read all key

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', 'w3big'=>'w3big.com', '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
w3big

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', 'w3big'=>'w3big.com', '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
w3big.com
google.com

Detecting element exists

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', 'w3big'=>'w3big.com', '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 ... ELSE statement in a later section we'll introduce specific.


Gets the hash size

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', 'w3big'=>'w3big.com', '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

Hash add or remove elements

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', 'w3big'=>'w3big.com', '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