Perl sort()

您所在的位置:网站首页 娅咪的歌词 Perl sort()

Perl sort()

2023-01-23 09:32| 来源: 网络整理| 查看: 265

Perl sort()

Introduction to Perl sort()

Perl sort() function is used to sort the list with or without using a method of sorting in Perl, the sort function is used to sort the array, hashes, list, and subroutine. The sort function is very useful and important to sort the list, array, and hashes. We can specify sort function in the form of subroutine or blocks in Perl, if we have not defined specified block or subroutine then sort function will take default method of sorting. The sort function sorts a list and returns the sorted list as the output to the user.

All in One Software Development Bundle(600+ Courses, 50+ projects) Python TutorialC SharpJavaJavaScriptC Plus PlusSoftware TestingSQLKali LinuxPrice View Courses 600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access 4.6 (85,824 ratings) Syntax and Parameters

Below is the syntax of the sort function in Perl is as follows.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

sort list; – Sorting list using sorting method. sort block list; – Sorting block list using sorting method. sort subroutine list; – Sorting subroutine list using sorting method.

Below is the parameter description syntax of Perl sort function is as follows.

Sort – Sort method is very important and useful in Perl to sort the block list and subroutine list. We can specify sort function in the form of subroutine or blocks in Perl, if we have not defined specified block or subroutine then sort function will take default method of sorting. List – List is defined in sort function to sort the list by using a sort function. The list parameter is very useful and important in sort function. Sort list is anything we can use it can be a character or integer number. Blocklist – This is defined in sort function to sort the numerical list by using a sort function. Block List parameter is very useful and important in sort function. To sort numerical data then we have used a block list. Subroutine list – This is defined in sort function to sort subroutine list by using a sort function. The subroutine List parameter is very useful and important in sort function. We have also use subroutine to sort using the sort function. How does sort() function work in Perl? Below is the working of sort function in Perl is as follows. Sort function will return the list as output as per the users’ requirement. Sort function will return the sorted list as per the user’s requirement. While sorting list using sort function, the result of sort function only return in sorted list original data will remain the same. Sort function will not affect original data. There are three forms of sort function in Perl is the list, block list, and subroutine list. We can sort the data of blocklist and subroutine list by using sort function, if we have not defined any sorting then sort method will use the default method of sorting. After passing the list to sort function then the result will get as a new sorted list to the user. This list is as per the alphabetical order. If we want to sort numerical data then we have used block list sort function, the block list will compare two values. The Blocklist is using a special package variable for comparison. For comparison of blocklist, we have used $a, $b in Perl. Sometimes sorting criteria is difficult at the same time we have using subroutine to sort the user data using sorting function. The sort function is very useful and important in Perl to sort list, array, and hashes function. Perl sort function is used to sort the list with or without using a method of sorting, the sort function is used to sort the array, hashes, list, and subroutine. The sort function sorts a list and returns the sorted list as the output to the user. We can specify this function in the form of subroutine or blocks, if we have not defined a specified block or subroutine then sort function will take the default method of sorting. Sort function will sort the list according to a subroutine name or any anonymous subroutine which was specified from the block. If we have not used any subroutine or block sort function will sort using normal alphabetical sequence. After specifying block or subroutine then the subroutine using sort function in Perl return an integer, greater than, less than or equal to zero, it will sort according to how elements of the array is sorted. Examples of Perl sort()

Below is the example of sort function:

Example #1 – Sorting list using alphabetically

The below example shows sort list alphabetically using the sort function. By default sort, the function will sort element of the list with string comparison in ascending order. The below example shows how to sort the list alphabetically are as follows.

Cod

use warnings; use strict; my @sort_list = sort qw(Perl in function sort);      ##Take input from user to sort list. my @sort_list_rev = sort qw(Sort function in perl); print "@sort_list\n";       ## Display sorted result using sorting function. print "@sort_list_rev\n";      ## Display sorted result using sorting function.

Output:

Perl sort() output 1

In the above example first time we have sort string name as “Perl in function sort” and the second time we have sort string as “Sort function in Perl”.

Example #2. Use the block to sort

In the below example we have used block to sort the order using the sort function. Block will follow the same method of sorting the order as numeric and character.

Code –

use warnings; use strict; # Use block list to sort list using sort function. my @block_sort = sort { $a $b } (10, 2, 5, 4, 3, 7, 6, 8, 9, 1); print "@block_sort\n";

Output:

Perl sort() output 2

Example #3. Use subroutine to sort

In the below example we have used subroutine to sort the order using the sort function. To sort the order we have also used subroutine in Perl.

Code

use warnings; use strict; # Use subroutine to sort array in perl. my @numerical_sort = sort compare_sort (22, 11, 34, 13, 39, 67); print "@numerical_sort\n"; sub compare_sort { if($a < $b) { return -1; } elsif($a == $b) { return 0; } else { return 1; } }

Output:

output 3

Conclusion

The sort function is very useful and important in Perl to sort list, array, and hashes function. We can specify this function in the form of subroutine or blocks in Perl, if we have not defined specified block or subroutine then sort function will take default method of sorting.

Recommended Articles

This is a guide to Perl sort(). Here we discuss the introduction to Perl sort() along with the working, appropriate syntax ,parameters and respective examples. You may also look at the following articles to learn more –

Perl Data Types Perl Array Perl Versions Perl Variables Popular Course in this category Perl Training (1 Courses)   1 Online Courses |  5+ Hours |  Verifiable Certificate of Completion |  Lifetime Access 4.5 Price View Course

Related Courses

Ruby on Rails Training (6 Courses, 4+ Projects)4.9Python Certifications Training Program (40 Courses, 13+ Projects)4.8PHP Training (5 Courses, 3 Project)4.7 1 SharesShareTweetShare Primary Sidebar


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3