Perl Hashes
w3hello.com logo
Perl Basics
Perl Introduction
Perl Installation
Perl Comments
Perl String
Perl Operators
Perl Variables
Perl Arrays
Perl do...while Loop
Perl Environment
Perl Syntax Overview
Perl while Loop
Perl Array with Loops
Perl IF...ELSE statement
Perl IF Statement
Perl if-else
Perl foreach Loop
Perl for Loop
Perl continue Statement
Perl nested Loop
Perl until Loop
Perl next Statement
Perl Array
Perl Loops
Perl File I/O
Perl goto
Perl Socket Programming
Perl Error Handling
Perl Directories
Perl Process Management
Perl stdin
Real Device Vs Emulator Testing: Ultimate Showdown
Perl Formats Writing Reports
Perl Regular Expressions
Perl Create Excel File
A complete guide to CSTE certification
Perl Hashes
Perl Special Variables
Perl do-while Loop
Perl Chop() Vs Chomp()
The Perl DBI
Perl IF...ELSIF statement
Perl print() and say()
Perl Database Access
Perl Formats
Perl last Statement
Perl UNLESS...ELSE statement
Perl Scalars
How to organize Requirements as a Business Analyst
Perl Packages and Modules
Perl goto Statement
Perl Object Oriented
Perl Subroutines
Perl redo Statement
Perl Command Line Arguments | argv
Perl String Escaping Characters
Perl Date and Time
Perl Modules and namespaces
Perl UNLESS...ELSIF statement
Perl Conditional Statements IF...ELSE
Perl Functions and Subroutines
Perl Sending Email
Perl DBI
Perl Versions
Top 100 QTP Interview Questions & Answers
Perl Operator Types
Perl switch Statement
Perl File handling
Perl References
Perl CGI Programming
Object Oriented Programming in PERL
Perl switch
Perl Interview Questions
How PERL is used in Automation Testing
Perl Functions References
Perl Multidimensional Array
Perl unless Statement
Perl Basics
Previous : A complete guide to CSTE certification Next : Perl Special Variables

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
Previous : A complete guide to CSTE certification Next : Perl Special Variables
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap