Introduction to Perl's Basic Array Sorting Method

Perl has a built-in function called sort that will definitely sort an array. In its simplest form, you pass an array and it returns the sorted array of elements. @sorted = sort @original.

Sort based on ASCII code

The copy code 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 print

The copy code code is as follows:


$VAR1 = [
        'foo',
        'bar',
        'care',
        'moo'
      ];
 
$VAR1 = [
        'bar',
        'foo',
        'moo',
        'care'
      ];


The first output shows the array before sorting, the second after sorting.

This is the simplest case, but it may not be what you want. Like, what if some words start with a capital letter?

The copy code code is as follows:


my @words = qw(foo bar Make moo);


The result in @sorted_names will be:

The copy code code is as follows:


$VAR1 = [
        'Care',
        'bar',
        'foo',
        'moo'
      ];


You'll notice that words starting with a capital letter come first. This is because sort sorts according to the ASCII code table by default, and all uppercase letters are sorted before lowercase letters.

comparison function

The way Perl's sort works is that it iterates over every two elements of the original array; each time it puts the left value into variable $a and the right value into variable $b. Then call the compare function. The "comparison function" returns 1 if the content of $a should be on the left, -1 if the content of $b should be on the left, and 0 if both are the same.

Usually you don't see the comparison function, sort compares the values ​​according to the ASCII code table, but if you want, you can write it explicitly:

The copy code code is as follows:


sort { $a cmp $b } @words;


This code will achieve the same effect as sort @words without using blocks.

Here you can see that by default perl uses cmp as the comparison function. This is because it is cmp that can do the work we need here. It compares the values ​​of the strings on both sides, and returns 1 if the left argument is "less than" the right argument, -1 if the left argument is "greater than" the right argument, and 0 if they are equal.

in alphabetical order

If you want to sort strings regardless of their case - commonly known as alphabetical order - you can do this like in the next example:

The copy code code is as follows:


my @sorted_words = sort { lc($a) cmp lc($b) } @words;


Here for comparison, we call the lc function to return the lowercase version of the argument. Then cmp compares these lowercase versions and decides which comes first from the original string.

turn out

The copy code code is as follows:


$VAR1 = [
        'bar',
        'foo',
        'moo',
        'Care'
      ];

Perl sort values

If you use sort to sort the numeric array by default, the result may not be what we expected.

The copy code 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
      ];


This is not surprising when you think about it. When the comparison function sees 12 and 3, it compares them as strings. This means compare the first characters "1" and "3" of the two strings. In the ASCII code table, "1" is before "3", so the string "12" will be sorted before the string "3".

Perl won't magically guess that you want to sort the values ​​numerically.

Although we can write a comparison function to compare two values ​​numerically. But here we use <=> (also known as the spaceship operator), which compares two arguments numerically and returns 1, -1 or 0.

The copy code code is as follows:


my @sorted_numbers = sort { $a <=> $b } @numbers;


turn out:

The copy code code is as follows:


$VAR1 = [
        2,
        3,
        12,
        14,
        23
      ];

Guess you like

Origin blog.csdn.net/jh035512/article/details/128141744