Open In App

Sorting mixed Strings in Perl

Improve
Improve
Like Article
Like
Save
Share
Report

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;
      
    # 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.




    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;  




#!/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.




#!/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


Last Updated : 14 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads