Sorting mixed Strings in Perl
Last Updated :
14 Mar, 2019
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 ways with the use of sort() function.
Example:
Input : Key_8, pub_12, pri_4 (array of strings)
Output : pri_4 Key_8 pub_12
Sorting of such arrays is done by extracting the numbers from the strings which can be done in two ways:
- Using substr() Function: For the purpose of comparing the strings using the numbers, it is very essential to get numbers from strings. Based on the numbers we are going to sort the array of strings. substr() function is used to extract these numbers from the string. This function takes, no. of characters in the string excluding the numeric, as the parameter.
Note: All the alphanumeric strings in the array must be of the same size.
Example:
PERL
use strict;
use 5.010;
# Defining array values with
# alphanumeric strings
my @x = qw(prin_4 Keys_8 pubg_12);
# Use of sort and substr function
# to sort the array
my @y = sort { substr($a, 5) <=> substr($b, 5) } @x;
# Printing the sorted array
print join " ", @y;
Output:
prin_4 Keys_8 pubg_12
-
Using Regular Expressions : Executing above code is a tough job if the alphanumeric string is a little bit complex, hence, for more simplicity, we can use Regular Expressions.
For Example- if the array consists "Keys_8_keys" then it's difficult to handle such case, hence for proper filtering of the number from the string, Regular Expressions are used.
Note: This method doesn't care if the alphanumeric strings are of different sizes.
PERL
use strict;
use 5.010;
# Sample string to extract
# number from
my $str = 'Key_8_key';
# Regular expression to extract the number
my ($number) = $str =~ /(\d+)/;
# Printing the extracted number
print "Number Extracted from Key_8_key is $number\n";
# Defining the array with
# Alphanumeric strings
my @x = qw(pri_4 Key_8_key pubg_12);
# Array before sorting
print "\nArray Before sorting\n";
print join " ", sort @x;
# Calling the sort function
# with Regular Expression
my @y = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } @x;
# Printing the Array after sorting
print "\n\nArray After sorting\n";
print join " ", @y;
Output:
Number Extracted from Key_8_key is 8
Array Before sorting
Key_8_key pri_4 pubg_12
Array After sorting
pri_4 Key_8_key pubg_12
String without a Number: If the array consists of strings in which some of the strings don't have a number in it, then 0 can be used in place of that number. To check if there is no number in the string, following code is used:
my @y = sort { (($a =~ /(\d+)/)[0] || 0) (($b =~ /(\d+)/)[0] || 0) } @x;
PERL
#!/usr/bin/perl
use strict;
use 5.010;
# Array with mixed type of strings
my @x = qw(pri_4 Key pubg_12);
# Function call with a Regular Expression
my @y = sort { (($a =~ /(\d+)/)[0] || 0) <=>
(($b =~ /(\d+)/)[0] || 0) } @x;
# Printing the sorted array
print join " ", @y;
Output:
Key pri_4 pubg_12
Sorting using a Subroutine: For better modularization of code we can create separate function or subroutine in Perl and perform the sorting operation with the use of that subroutine. sort() function here takes the subroutine name as the parameter and calls the subroutine which holds the definition for sorting.
PERL
#!/usr/bin/perl
use strict;
use 5.010;
# Defining an array with strings
my @x = qw(Key_8 pri_4 pubg_12);
# Calling sort function to sort
# array using subroutine
my @y = sort numbersort @x;
# Printing the sorted array
print join " ", @y;
# Subroutine to sort the array
sub numbersort
{
my ( $anum ) = $a =~ /(\d+)/;
my ( $bnum ) = $b =~ /(\d+)/;
( $anum || 0 ) <=> ( $bnum || 0 );
}
Output:
pri_4 Key_8 pubg_12
Similar Reads
Sorting Hash in Perl 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 examp
6 min read
Perl | String functions (length, lc, uc, index, rindex) String in Perl is a sequence of character enclosed within some kinds of quotation marks. Perl string can contain UNICODE, ASCII and escape sequence characters. Perl provides the various function to manipulate the string like any other programming language. Some string functions of Perl are as follow
4 min read
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 | Multi-line Strings | Here Document A string in Perl is a scalar variable 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. Multi-line string is required in a scenario where the user wants a complete paragraph or a group of paragraphs as a st
4 min read
Perl | Useful String functions 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 (â). Perl provid
3 min read