In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use the sort () function in Perl. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Perl has a built-in function called sort that can no doubt sort an array. Its simplest form is to pass an array that returns an array of sorted elements. @ sorted = sort @ original.
Sort based on ASCII code #! / usr/bin/perluse strict;use warnings;use 5.010 for 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
$VAR1 = ['foo',' bar', 'zorg',' moo']; $VAR1 = ['bar',' foo', 'moo',' zorg']
The first output shows the array before sorting, and the second is sorted.
This is the simplest situation, but it may not be what you want. For example, what if some words begin with capital letters?
My @ words = qw (foo bar Zorg moo)
The result in @ sorted_names will be:
$VAR1 = ['Zorg',' bar', 'foo',' moo']
You will find that words that begin with capital letters rank first. This is because sort is sorted by default according to the ASCII code table, and all uppercase letters come before lowercase letters.
Comparison function
Perl's sort works like this by iterating through every two elements of the original array; each time you put the value on the left in the variable $an and the value on the right in the variable $b. Then call the compare function. The comparison function returns 1 if the content of $a should be on the left,-1 if $b should be on the left, and 0 if both are the same.
Usually you don't see the comparison function, and sort compares the values according to the ASCII code table, but you can write it explicitly if you want:
Sort {$a cmp $b} @ words
This code will have the same effect as sort @ words without using blocks.
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 we need here. It compares the values of strings on both sides, returns 1 if the left parameter is "less than" the right parameter,-1 if the left parameter "greater than" the right parameter, and 0 if equal.
In alphabetical order
If you want to sort the string by ignoring the case of the string-- commonly known as alphabetical order-- you can do this like the next example:
My @ sorted_words = sort {lc ($a) cmp lc ($b)} @ words
For comparison here, we call the lc function to return a lowercase version of the parameter. Cmp then compares these lowercase versions and determines who comes first and who comes later in the original string.
As a result,
$VAR1 = ['bar',' foo', 'moo',' Zorg']; Perl sorts the values
If you use sort to sort numeric arrays by default, the result may not be what we expect.
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, this is not surprising. The comparison function sees 12 and 3, and it compares by string. This means comparing the first character "1" and "3" of the two strings. In the ASCII table, "1" comes before "3", so the string "12" precedes 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 spaceship operator), which compares two parameters by number and returns 1,-1, or 0.
My @ sorted_numbers = sort {$a $b} @ numbers
The result is:
$VAR1 = [2, 3, 12, 14, 23]; this is the end of the article on "how to use the sort () function in Perl". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.