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

#!/usr/bin/perl %data = ('google', 'google.com', 'example', 'example.com', 'taobao', 'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'example'} = $data{'example'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'example'} = $data{'example'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n";

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

#!/uer/bin/perl %data = (-taobao => 45, -google => 30, -example => 40); @array = @data{-taobao, -example}; print "Array : @array\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); @urls = values %data; print "$urls[0]\n"; print "$urls[1]\n"; print "$urls[2]\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); if( exists($data{'facebook'} ) ){ print "The URL of facebook is$data{'facebook'} \n"; } else { print "The facebook key does not exist\n"; }

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - Hash size:$size\n"; @values = values %data; $size = @values; print "2 - Hash size:$size\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); @keys = keys %data; $size = @keys; print "1 - Hash size:$size\n"; # Add element $data{'facebook'} = 'facebook.com'; @keys = keys %data; $size = @keys; print "2 - Hash size:$size\n"; # Delete elements in the hash delete $data{'taobao'}; @keys = keys %data; $size = @keys; print "3 - Hash size:$size\n";

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

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); foreach $key (keys %data){ print "$data{$key}\n"; }

Example - Using while

#!/usr/bin/perl %data = ('google'=>'google.com', 'example'=>'example.com', 'taobao'=>'taobao.com'); while(($key, $value) = each(%data)){ print "$data{$key}\n"; }

Executing the above program produces the following output:

example.com
google.com
taobao.com
Other Extensions