Home  >  Article  >  Backend Development  >  How to sort mixed strings in Perl? (code example)

How to sort mixed strings in Perl? (code example)

青灯夜游
青灯夜游Original
2019-04-16 11:40:233127browse

Sorting in Perl can be done using the predefined function "sort"; this function sorts the array passed to it using the quicksort algorithm. The following article will introduce to you how to use the sort() function to sort arrays containing mixed-form strings (i.e. alphanumeric strings) in various ways. I hope it will be helpful to you. [Video tutorial recommendation: Perl tutorial]

How to sort mixed strings in Perl? (code example)

Method 1: sort() substr() function

In order to compare strings using numbers, it is very important to get the number from the string. We can sort the string array based on these numbers.

The substr() function can be used to extract these numbers from a string. This function takes as argument the number of characters in the string excluding numbers.

Note: All alphanumeric strings in the array must be the same size.

Example:

use strict; 
use 5.010; 
  
# 用字母数字字符串定义数组值
my @x = qw(prin_4 Keys_8 pubg_12); 
print "原数组:\n";
print join " , ", @x;  
# 使用sort()和substr()函数对数组进行排序 
my @y = sort { substr($a, 5) <=> substr($b, 5) } @x; 
  
# 输出排序的数组
print "\n\n排序的数组:\n";
print join " , ", @y;

Output:

原数组:
prin_4 , Keys_8 , pubg_12

排序的数组:
prin_4 , Keys_8 , pubg_12

Method 2: sort() Regular expression

If the alphanumeric string is a bit complex, executing the above code is a difficult job, so to make it simpler, we can use regular expressions.

For example: If the array contains "Keys_8_keys", then it is difficult to handle this case, so in order to filter the numbers in the string correctly, you can use regular expressions.

Note: This method does not care if the alphanumeric strings are of different sizes.

Example:

use strict; 
use 5.010; 
  
# Sample string to extract  
# number from 
my $str = &#39;Key_8_key&#39;; 
  
# Regular expression to extract the number 
my ($number) = $str =~ /(\d+)/; 
  
# 输出提取的数字
print "从Key_8_key中提取的数字是:$number\n"; 
  
# 用字母数字字符串定义数组
my @x = qw(pri_4 Key_8_key pubg_12); 
  
# 排序前的数组
print "\n排序前的数组:\n"; 
print join " , ", sort @x; 
  
# 使用正则表达式
my @y = sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } @x; 
  
# 排序后数组
print "\n\n排序后数组\n"; 
print join " , ", @y;

Output:

从Key_8_key中提取的数字是:8
排序前的数组:
Key_8_key , pri_4 , pubg_12
排序后数组
pri_4 , Key_8_key , pubg_12

Note: If the array contains strings where some of the strings do not have numbers in them, you can use 0 replaces this number. To check if there are no numbers in the string, use the following code:

my @y = sort { (($a =~ /(\d+)/)[0] || 0)  (($b =~ /(\d+)/)[0] || 0) } @x;

Example:

#!/usr/bin/perl 
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 "排序后数组:\n"; 
print join " , ", @y;

Output:

排序后数组:
Key , pri_4 , pubg_12

The above is the entire content of this article, I hope it can It will be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to sort mixed strings in Perl? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn