Introduction to Perl basic array sorting methods
This article describes how to use Perl to sort strings or numeric arrays.
Perl has a built-in function called sort which can undoubtedly sort an array. The simplest form is to pass an array, which returns an array composed of sorted elements. @ Sorted = sort @ original.
ASCII-based sorting
The Code is as follows:
#! /Usr/bin/perl
Use strict;
Use warnings;
Use 5.010;
Use Data: Dumper qw (Dumper );
My @ words = qw (foo bar zorg moo );
Say Dumper \ @ words;
My @ sorted_words = sort @ words;
Say Dumper \ @ sorted_words;
The above example will be printed
The Code is as follows:
$ VAR1 = [
'Foo ',
'Bar ',
'Zorg ',
'Moo'
];
$ VAR1 = [
'Bar ',
'Foo ',
'Moo ',
'Zorg'
];
The first output shows the array before sorting, and the second output is after sorting.
This is the simplest case, but it may not be what you want. For example, what if some words start with an uppercase letter?
The Code is as follows:
My @ words = qw (foo bar Zorg moo );
@ Sorted_names:
The Code is as follows:
$ VAR1 = [
'Zorg ',
'Bar ',
'Foo ',
'Moo'
];
You will find that the words starting with an uppercase letter come first. This is because sort is sorted by ASCII code table by default, and all uppercase letters are placed in front of lowercase letters.
Comparison Functions
Perl sort works in this way. It traverses every two elements of the original array. Each time, the value on the left is put into variable $ a, and the value on the right is put into variable $ B. Then, call the comparison function. If the content of $ a is on the left, the "comparison function" returns 1; if $ B is on the left,-1 is returned. If the two are the same, 0 is returned.
Generally, you cannot see the comparison function. sort compares values according to the ASCII code table. However, if you want to, you can explicitly write the following:
The Code is as follows:
Sort {$ a cmp $ B} @ words;
This code will achieve the same effect with sort @ words without blocks.
Here you can see that the default perl uses cmp as a comparison function. This is because cmp can do what we need here. It compares the values of strings on both sides. If the parameter on the left is "less than", 1 is returned. If the parameter on the left is "greater than",-1 is returned. If the parameter on the left is equal, returns 0.
In alphabetical order
If you want to ignore the case sensitivity of strings for sorting-that is, the so-called alphabetic order, you can do this as in the next example:
The Code is as follows:
My @ sorted_words = sort {lc ($ a) cmp lc ($ B)} @ words;
For comparison, we call the lc function to return the lower-case version of the parameter. Then cmp compares these lower-case versions and determines who is the first character string.
The result is
The Code is as follows:
$ VAR1 = [
'Bar ',
'Foo ',
'Moo ',
'Zorg'
];
Perl sorts numeric values
If you use sort to sort the numeric array by default, the result may not be expected.
The Code is as follows:
My @ numbers = (14, 3, 12, 2, 23 );
My @ sorted_numbers = sort @ numbers;
Say Dumper \ @ sorted_numbers;
$ VAR1 = [
12,
14,
2,
23,
3
];
If you think about it carefully, it's not surprising. When the comparison function shows 12 and 3, it compares by string. This means that the first character "1" and "3" of the two strings are compared ". In the ASCII code table, "1" is at the front of "3", so "12" is placed before "3.
Perl won't magically guess that you want to sort these values by numbers.
Although we can write a comparison function to compare two values by number. But here we use <=> (also known as the spacecraft operator), which compares two parameters by number and returns 1,-1, or 0.
The Code is as follows:
My @ sorted_numbers = sort {$ a <=> $ B} @ numbers;
The result is:
The Code is as follows:
$ VAR1 = [
2,
3,
12,
14,
23
];