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:
use strict;
use 5.010;
my @x = qw(prin_4 Keys_8 pubg_12) ;
my @y = sort { substr ( $a , 5) <=> substr ( $b , 5) } @x ;
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.
use strict;
use 5.010;
my $str = 'Key_8_key' ;
my ( $number ) = $str =~ /(\d+)/;
print "Number Extracted from Key_8_key is $number\n" ;
my @x = qw(pri_4 Key_8_key pubg_12) ;
print "\nArray Before sorting\n" ;
print join " " , sort @x ;
my @y = sort { ( $a =~ /(\d+)/)[0] <=> ( $b =~ /(\d+)/)[0] } @x ;
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;
use strict;
use 5.010;
my @x = qw(pri_4 Key pubg_12) ;
my @y = sort { (( $a =~ /(\d+)/)[0] || 0) <=>
(( $b =~ /(\d+)/)[0] || 0) } @x ;
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.
use strict;
use 5.010;
my @x = qw(Key_8 pri_4 pubg_12) ;
my @y = sort numbersort @x ;
print join " " , @y ;
sub numbersort
{
my ( $anum ) = $a =~ /(\d+)/;
my ( $bnum ) = $b =~ /(\d+)/;
( $anum || 0 ) <=> ( $bnum || 0 );
}
|
Output:
pri_4 Key_8 pubg_12
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Mar, 2019
Like Article
Save Article