How to sort mixed strings in Perl? (code example)
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]
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 = 'Key_8_key'; # 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

Outlook offers many settings and features to help you manage your work more efficiently. One of them is the sorting option that allows you to categorize your emails according to your needs. In this tutorial, we will learn how to use Outlook's sorting feature to organize emails based on criteria such as sender, subject, date, category, or size. This will make it easier for you to process and find important information, making you more productive. Microsoft Outlook is a powerful application that makes it easy to centrally manage your email and calendar schedules. You can easily send, receive, and organize email, while built-in calendar functionality makes it easy to keep track of your upcoming events and appointments. How to be in Outloo

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Sorting methods in excel: 1. Single column sorting; 2. Multiple column sorting; 3. Custom sorting. Detailed introduction: 1. Single-column sorting is the most common sorting method. It sorts according to a selected column; 2. Multi-column sorting refers to sorting data in multiple columns, usually sorting according to a certain column first. On the basis of, sort by another column; 3. Custom sorting, allowing users to define the sort order according to their own needs.

WPS is a very complete office software, including text editing, data tables, PPT presentations, PDF formats, flow charts and other functions. Among them, the ones we use most are text, tables, and demonstrations, and they are also the ones we are most familiar with. In our study work, we sometimes use WPS tables to make some data statistics. For example, the school will count the scores of each student. If we have to manually sort the scores of so many students, it will be really a headache. In fact, we don’t have to worry, because our WPS table has a sorting function to solve this problem for us. Next, let’s learn how to sort WPS together. Method steps: Step 1: First we need to open the WPS table that needs to be sorted
