In this article we learned how to sort strings or arrays of numbers in Perl.
Perl has a built-in function called sort. There is no doubt that you can sort an array. The simplest form is to pass an array that returns an array of sorted elements. @sorted = Sort @original.
Sort based on ASCII code
#!/usr/bin/perl
Use strict;
Use warnings;
Use 5.010;
Use Data::D umper qw (dumper);
My @words = QW (foo bar zorg moo);
Say dumper \ @words;
My @sorted_words = sort @words;
Say dumper \ @sorted_words;
The example above will be printed
$VAR 1 = [
' Foo ',
' Bar ',
' Zorg ',
' Moo '
];
$VAR 1 = [
' Bar ',
' Foo ',
' Moo ',
' Zorg '
];
The first output shows the array before the sort, and the second one is sorted.
This is the simplest case, but it may not be what you want. For example, what if some words start with an uppercase letter?
My @words = QW (foo bar zorg moo);
The results in @sorted_names will be:
$VAR 1 = [
' Zorg ',
' Bar ',
' Foo ',
' Moo '
];
You'll find that words that start with a capital letter are ranked first. This is because sort defaults to the ASCII table and all uppercase letters are in front of the lowercase letters.
comparison function
Perl's sort works by traversing every two elements of the original array, putting the left value into the variable $a each time, and putting the value on the right to the variable $b. The comparison function is then called. If the $a content should be on the left, the comparison function returns 1, and if $b should be on the left, return-1, the same, and return 0.
Usually you don't see the comparison function, sort will compare the values against the ASCII table, but if you want, you can write them explicitly:
This code will have the same effect as the sort @words without using a block.
As you can see here, the default Perl uses CMP as the comparison function. This is because it is CMP that can do the work that we need. It compares the values of the strings on both sides, returns 1 if the left argument is less than the right argument, and returns 1 if the left argument is greater than the right argument, or 0 if it is equal.
Alphabetical order
If you want to ignore the case of strings to sort-that is, what is usually called the alphabetical order, you can do this like the next example:
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. CMP then compares these lowercase versions and determines the original string who is first.
The result is
$VAR 1 = [
' Bar ',
' Foo ',
' Moo ',
' Zorg '
];
Perl to sort numerically
If you use sort for the default sort on a numeric array, the result may not be what we expect.
My @numbers = (14, 3, 12, 2, 23);
My @sorted_numbers = sort @numbers;
Say dumper \ @sorted_numbers;
$VAR 1 = [
12,
14,
2,
23,
3
];
If you think about it, it's not surprising. Comparison functions See 12 and 3 o'clock, which are compared by string. This means comparing the first character "1" and "3" of the two strings. In the ASCII code table, "1" precedes "3", so the string "12" is preceded by the string "3".
Perl doesn't magically guess that you want to sort these values by number.
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.
My @sorted_numbers = sort {$a <=> $b} @numbers;
The result:
$VAR 1 = [
2,
3,
12,
14,
23
];