Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Hashes

Perl Hashes

A hash is a key/value collection of pairs.

In Perl, a hash variable is marked with a percent sign (%) at the beginning.

The format for accessing hash elements:${key}。

The following is a simple hash example:

Example

#!/usr/bin/perl

%data

=

(

'

google

'

,

'

google.com

'

,

'

runoob

'

,

'

runoob.com

'

,

'

taobao

'

,

'

taobao.com

'

)

;

print

"

$

data{'google'} =

$data

{'google'}

\n

"

;

print

"

$

data{'runoob'} =

$data

{'runoob'}

\n

"

;

print

"

$

data{'taobao'} =

$data

{'taobao'}

\n

"

;

Running the above program, the output is:


Creating a hash

A hash can be created in the following two ways:

1. Set a value for each key



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

$data{'runoob'} = 'runoob.com';

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

2. Set through a list

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

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

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

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

The following example is a variation of the above example, using - instead of quotes:

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

Using this approach, keys cannot contain spaces; the way to read an element is:



$val = $data{-google}

$val = $data{-runoob}


Accessing hash elements

The format for accessing hash elements:${key}; the example is as follows:

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

print

"

$

data{'google'} =

$data

{'google'}

\n

"

;

print

"

$

data{'runoob'} =

$data

{'runoob'}

\n

"

;

print

"

$

data{'taobao'} =

$data

{'taobao'}

\n

"

;

Running the above program, the output is:


Reading hash values

You can extract values from a hash just like from an array.

The syntax format for extracting hash values into an array:@{key1,key2}。

Example

#!/uer/bin/perl

%data

=

(

taobao

=>

45

, -

google

=>

30

, -

runoob

=>

40

)

;

@array

=

@data

{

taobao

, -

runoob

}

;

print

"

Array : @array

\n

"

;

Running the above program, the output is:



Array : 45 40


Reading the keys and values of a hash

Reading all keys

We can use the keys function reads all keys of a hash; the syntax format is as follows:



keys %HASH

This function returns an array of all keys in the hash.

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

@names

=

keys

%data

;

print

"

$names

[0]

\n

"

;

print

"

$names

[1]

\n

"

;

print

"

$names

[2]

\n

"

;

Running the above program, the output is:



taobao

google

runoob

Similarly, we can use the values function to read all values of a hash; the syntax format is as follows:



values %HASH

This function returns an array of all values in the hash.

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

@urls

=

values

%data

;

print

"

$urls

[0]

\n

"

;

print

"

$urls

[1]

\n

"

;

print

"

$urls

[2]

\n

"

;

Running the above program, the output is:



taobao.com

runoob.com

google.com


Checking whether an element exists

If you read a key/value pair that does not exist in the hash, it will return undefined value, and a warning will be given during execution.

To avoid this, we can use the exists function to determine whether a key exists and read it when it does:

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.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

"

;

}

Running the above program, the output is:



facebook 键不存在

In the above code we used the IF...ELSE statement, which we will introduce in detail in later chapters.


Getting the hash size

The hash size is the number of elements. We can obtain the hash size by first getting an array of all key or value elements and then counting the number of array elements, as shown in the following example:

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.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

"

;

Running the above program, the output is:



1 - 哈希大小: 3

2 - 哈希大小: 3


Adding or deleting elements in a hash

Adding a key/value pair can be done by simple assignment. But to delete a hash element you need to use the delete functions as follows:

Example

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

@keys

=

keys

%data

;

$size

=

@keys

;

print

"

1 - hash size:

$size

\n

"

;

Adding elements

$data

{

'

facebook

'

}

=

'

facebook.com

'

;

@keys

=

keys

%data

;

$size

=

@keys

;

print

"

2 - hash size:

$size

\n

"

;

Delete an element from the hash

delete

$data

{

'

taobao

'

}

;

@keys

=

keys

%data

;

$size

=

@keys

;

print

"

3 - hash size:

$size

\n

"

;

Running the above program, the output is:



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

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

foreach

$key

(

keys

%data

)

{

print

"

$data

{

$key

}

\n

"

;

}

Example - Using while

#!/usr/bin/perl

%data

=

(

'

google

'

=>

'

google.com

'

,

'

runoob

'

=>

'

runoob.com

'

,

'

taobao

'

=>

'

taobao.com

'

)

;

while

(

(

$key

,

$value

)

=

each

(

%data

)

)

{

print

"

$data

{

$key

}

\n

"

;

}

Running the above program, the output is:



runoob.com

google.com

taobao.com