Perl: Functions and sorting

Source: Internet
Author: User

Function (subroutine):

Function brackets in Perl can be written or not, unless the meaning is not affected.

 

Custom subroutine:

sub function-name {

    expressions;

}

 

Subroutine return value:

All perl subroutines have a return value. The result of the last statement operation in the subroutine is the program's return value by default.

The return operator returns a value immediately from a subroutine.

 

Subroutine parameters:

To pass parameters to a subroutine, just add parentheses and parameters after the subroutine when calling the subroutine.

Perl stores the parameter list in a special array variable @_, and the subroutine gets the entire variable to get the number of parameters and the value of the parameter.

This special array variable is valid only during subroutine execution. Use $ _ [n] in the subroutine to indicate the value of each parameter.

@_ is a private variable of a subroutine, and each subroutine has its own @_.

 

Subroutine private parameters:

The default variables in Perl are global variables.

You can use the my operator to create private lexical variables and arrays (which actually have the values of a list).

my arg1, arg2; #my can only private one variable arg1, arg2 will not be privatized.

my (arg1, arg2); #Using parentheses can also private variables in parentheses.

Private variables and arrays are only valid in the current block (subroutine, control flow, etc.).

Lexical variables cannot retain the value of a variable.

It is recommended to use my for the first occurrence of all parameters

my ($ arg1, $ arg2,…);

($ arg1, $ arg2,…) = @_;

Equivalent to:

my (arg1, arg2,…) = @_; #Create private variables directly and assign values.

 

Long parameter list:

sub function {

    If (@_! = 2) {

        Print “Warning! The argument is wrong. \ N”;

}

 

Persistent private variables:

The lexical variable declared with my cannot save the value of the variable, and each call to this lexical variable may be assigned a different value.

Use state to declare a variable, this variable is still private, but you can retain the value of the variable during multiple subroutine calls.

state $ arg / @ arr;

 

Call subroutine:

If the definition of a subroutine can be written before calling it, it can be like calling a built-in function:

function arg1, arg2;

It is recommended that all subroutine calls use & parameters with parentheses:

& function (arg1, arg2,…);

If a custom word function and a built-in function use the same name and call a custom function, a function that does not use & calls a built-in function.

 

--------------------------------------------------

Sorting sub-functions:

 

Number sort

sub by_number {$ a <=> $ b}

my @result = sort by_number @numbers;

Equivalent to:

my @result = sort {$ a <=> $ b} @numbers; #Sort from small to large

my @result = reverse sort {$ a <=> $ b} @numbers; #Sort from big to small

my @result = sort {$ b <=> $ a} @numbers; #Sort from big to small

 

Sorting strings

sub ASCIIbeticaly {$ a cmp $ b}

sort ASCIIbetically @strings;

 

Convert to lowercase sort

sub case_insensitive {"\ L $ a" cmp "\ L $ b"}

sort case_insensitive @strings;

 

Sorting hash values:

sub by_value {$ hash {$ b} <=> $ hash {$ a}} #The values of the hash are sorted from large to small.

my @result = sort by_value keys% hash;

foreach my $ keys (@result) {

    print "$ keys => $ score {$ keys} \ n";

}

 

sub by_value_and_key {

    $ hash {$ b} <=> $ hash {$ a}

        or

   $ a cmp $ b

}

my @result = sortby_value_and_key keys% hash;

perl: functions and sorting

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.