Prerequisite: Perl | Hashes Set of key/value pair is called a Hash. Each key in a hash structure are unique and of type strings. The values associated with these keys are scalar. These values can either be a number, string or a reference. A Hash is declared using my keyword. Let us consider an example to understand the concept of sorting the hash.
Example: Consider a hash with the names of the students in a class and with their average score. Here, the name of the students are the keys and their average scores are the values. Print this hash using foreach loop on the student names returned by the keys function.
Perl
# Perl program to demonstrate
# the concept of hash
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
# displaying the keys and values of Hash
# using the foreach loop and keys function
# output may be different each
# time when you run the code
foreach my $name (keys %studentnames) {
# printing the keys and values of hash
printf "%-8s %s\n", $name, $studentnames{$name};
}
Output:
Marty 16.5
Jason 25.2
Plato 39
Vivek 27
Socrates 29.5
Martha 14
Earl 31
Uri 19.6
Nitin 30
Time Complexity: O(n)
Auxiliary Space: O(n)
Note: The output may contain any random order depends on the system and the version of Perl. Hashes can be sorted in many ways as follows:
- Sorting the Hash according to the ASCII values of its keys: Generally, sorting is based on ASCII table. It means sorting will keep all the upper-case letters in front of all the lower-case letters. This is the default behavior of the sorting. The above example code is sorted according to the ASCII values of its keys.
- Sorting the Hash according to the alphabetical order of its keys: Here, the keys are sorted alphabetically.
Example:
Perl
# Perl program to demonstrate
# sorting of the hash according
# alphabetical order of its keys
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
# sorting the hash according
# alphabetical order of its keys
foreach my $name (sort {lc $a cmp lc $b} keys %studentnames)
{
printf "%-8s %s\n", $name, $studentnames{$name};
}
Output:
Earl 31
Jason 25.2
Martha 14
Marty 16.5
Nitin 30
Plato 39
Socrates 29.5
Uri 19.6
Vivek 27
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Sorting according to the values of Hash: You can also sort the hash according to the values of hash as follows:
Example 1: Sorting the hash values according to the ASCII table.
Perl
# Perl program to demonstrate
# sorting of the hash according
# to the values of Hash
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
# sorting of the hash according
# to ASCII code of values of Hash
foreach my $name (sort values %studentnames)
{
say $name;
}
14
16.5
19.6
25.2
27
29.5
30
31
39
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Example 2: Sorting according to the numerical value of Values of hash as follows:
Perl
# Perl program to demonstrate
# sorting of the hash according
# to the values of Hash
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 30,
Plato => 39,
);
# sorting of the hash according
# to the numerical value of
# Values of hash
foreach my $name (sort {$a<=>$b} values %studentnames)
{
say $name;
}
14
16.5
19.6
25.2
27
29.5
30
31
39
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Sort the keys of the hash according to the values: You can also sort the keys of hash according to the given values. Example 1: In the below program, <=> is termed as the spaceship operator. If you will run the code again and again then you can notice the difference in the output. Sometimes you find Plato before the Nitin and vice-versa.
Perl
# Perl program to demonstrate the
# Sorting of keys of the hash
# according to the values
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 45,
Plato => 45,
);
# Sort the keys of the hash
# according to the values
# Here $a and $b are the
# placeholder variable of sort
foreach my $name (sort {$studentnames{$a} <=>
$studentnames{$b}} keys %studentnames)
{
printf "%-8s %s\n", $name, $studentnames{$name};
}
Martha 14
Marty 16.5
Uri 19.6
Jason 25.2
Vivek 27
Socrates 29.5
Earl 31
Plato 45
Nitin 45
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Example 2: To solve the above code, keys having the same values can be sorted according to the ASCII table as follows:
Perl
# Perl program to demonstrate the
# Sorting of keys of the hash
# according to the values
use strict;
use warnings;
use 5.010;
# Creating a hash of studentnames
# and their average score
my %studentnames = (
Martha => 14,
Vivek => 27,
Earl => 31,
Marty => 16.5,
Jason => 25.2,
Socrates => 29.5,
Uri => 19.6,
Nitin => 45,
Plato => 45,
);
# keys that have the same value
# will be sorted according the
# ASCII table
foreach my $name (sort { $studentnames{$a} <=> $studentnames{$b} or
$a cmp $b } keys %studentnames) {
printf "%-8s %s\n", $name, $studentnames{$name};
}
Output:
Martha 14
Marty 16.5
Uri 19.6
Jason 25.2
Vivek 27
Socrates 29.5
Earl 31
Nitin 45
Plato 45
Explanation: Here you can see the key Nitin and Plato are sorted according to the ASCII table. It doesn't matter how many times you run the code, the output will remain the same.
Time Complexity: O(n logn)
Auxiliary Space: O(n)
Similar Reads
Perl | Sorting of Arrays Perl has a built-in sort() function to sort an array of alphabets and numbers. When an array is passed to the sort() function it returns a sorted array. Syntax: sort @Array Returns: a sorted array Sorting of Arrays in Perl can be done in multiple ways: Use of ASCII values to sort an Array Use of Com
5 min read
Perl | sort() Function sort() function in Perl is used to sort a list with or without the use of method of sorting. This method can be specified by the user in the form of subroutines or blocks. If a subroutine or block is not specified then it will follow the default method of sorting. Syntax: sort List sort block, List
2 min read
Perl | String Operators Operators are the foundation of any programming language. Thus, the functionality of Perl programming language is incomplete without the use of operators. A user can define operators as symbols that help to perform specific mathematical and logical computations on operands. String are scalar variabl
4 min read
Sorting mixed Strings in Perl Sorting in Perl can be done with the use of a pre-defined function 'sort'. This function uses a quicksort algorithm to sort the array passed to it. Syntax: sort @array Returns: a sorted array Sorting of an array that contains strings in the mixed form i.e. alphanumeric strings can be done in various
4 min read
Perl Programming Language Perl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++. Since Perl is a lot similar to other widely used lan
3 min read
Perl | rindex() Function rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given
2 min read
Perl | Useful String Operators A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (â) or double quote (â). Operators a
3 min read
Perl | Hash in Scalar and List Context Prerequisite: Perl Hash Hash in Perl is a set of key/value pairs. Perl provides us the flexibility to assign the hash to a List type and a Scalar type, known as LIST Context and SCALAR Context respectively. Hash in LIST Context The assignment of a hash to a list type in Perl is accomplished by makin
5 min read
Perl | Implementing a Stack Stack in Perl is a linear data structure that follows the LIFO (Last In First Out) or FILO (First In Last Out) order. In simpler terms, a stack is an array in which insertion and deletion takes place at only one end called the top of the stack. Pushing is the process of insertion of elements into a
4 min read
Perl | Comparing Scalars Prerequisite: Scalars in Perl Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars. There are two types of sets of Perl comparison operators. One is for numeric scalar values and one is for string sc
6 min read