Perl Array Sort Learning notes _perl

Source: Internet
Author: User
Tags array sort comparison lowercase uppercase letter


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


 code as follows:

#!/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
 code as follows:

$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?


code as follows:

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:


 code as follows:

Sort {$a CMP $b} @words;

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:


 code 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. CMP then compares these lowercase versions and determines the original string who is first.





The result is


 code as follows:

$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.


 code as follows:

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.


 code as follows:

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





The result:


code as follows:

$VAR 1 = [
2,
3,
12,
14,
23
];




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.