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 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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;

    chevron_right

    
    

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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;

    chevron_right

    
    

    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;  
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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 );
}

chevron_right


Output:

pri_4 Key_8 pubg_12


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.