Perl Hashes

The hashes is the most essential and influential part of the perl language. A hash is a group of key-value pairs. The keys are unique strings and values are scalar values.

Hashes are declared using my keyword. The variable name starts with a (%) sign.

Hashes are like arrays but there are two differences between them. First arrays are ordered but hashes are unordered. Second, hash elements are accessed using its value while array elements are accessed using its index value.

No repeating keys are allowed in hashes which makes the key values unique inside a hash. Every key has its single value.

Syntax:


Perl Hash Accessing

To access single element of hash, ($) sign is used before the variable name. And then key element is written inside {} braces.

Output:

New Delhi
Seoul
Washington, D.C.
Canberra

Perl Hash Indexing

Hashes are indexed using $key and $value variables. All the hash values will be printed using a while loop. As the while loop runs, values of each of these variables will be printed.

Output:

Australia, Canberra
India, New Delhi
USA, Washington, D.C.
South Korea, Seoul

Perl sorting Hash by key

You can sort a hash using either its key element or value element. Perl provides a sort() function for this. In this example, we'll sort the hash by its key elements.

Output:

Australia: Canberra
India: New Delhi
South Korea: Seoul
USA: Washington: D.C.

Look at the output, all the key elements are sorted alphabetically.


Perl sorting Hash by its value

Here we'll sort hash by its value elements.

Output:

UK London
India New Delhi
South Korea Seoul
USA Washington D.C.

Look at the output, all the value elements are sorted alphabetically.


Perl Hash key Existence

Accessing a key-value pair from hash which doesn't exist will return error or warnings. To prevent from this, you can check whether a key exist or not in a hash with exists() function. It returns true if the key exists.

Output:

found the key

The above output shows that the 'Indis' key exists in the 'capitals' hash.


Perl Hash Slices

If you want only some values from a hash, you can extract them and display as a list of values.

For this, you have to store them in an array variable with @ prefix as they will return a list of values and then print them.

Output:

New Delhi Washington, D.C. Canberra

Perl Hash creating Empty hash

An empty hash will always have size 0.

In this example, first we have created a hash with size 3. Then we have created an empty hash with size 0.

Output:

hash size: 3
hash size: 0

Perl Adding Hash Elements

New key-value pair can be added in a hash by declaring them as single element in the hash variable.

Here, we are adding two key-value pair, [Germany - Berlin] and [UK - London].

Output:

UK, London
Australia, Canberra
Germany, Berlin
India, New Delhi
USA, Washington D.C.
South Korea, Seoul

Perl Removing Hash Elements

To remove a hash element, use delete() function.

Here, we have removed both the key-value pairs which were added in the last example.

Output:

Australia, Canberra
India, New Delhi
USA, Washington D.C.
South Korea, Seoul

Perl deleting Vs Undefining Hash Elements

deleting: In deleting, key-value pair will be deleted from the hash.

Syntax:

undef: In undef, the value will be undefined but key will remain in the hash.

Syntax:

In the following example, we have created a hash 'rank'. One by one we'll undefine and remove all the key values from the hash. On undefining a key only its value will be shown, on deleting a key it will be completely deleted from the hash along with its value.

Like this, at the end all the hash elements will be deleted.

Output:

before:
John5 Jassi1 Jiyaa3 Ana7
undefine John
JohnJassi1 Jiyaa3 Ana7
remove John
Jassi1 Jiyaa3 Ana7
undefine Ana
Jassi1 Jiyaa3 Ana
remove Ana
Jassi1 Jiyaa3
undefine Jiyaa
Jassi1 Jiyaa
remove Jiyaa
Jassi1
undefine Jassi 
Jassi
remove Jassi




Latest Courses