I once read the following Perl subroutine
sub min{
(sort {$a<=>$b;} @_)[0];
}
How to understand the usage of sort and @_ here? What does [0] stand for?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(...)[0]returns the first element of the list inside of the parentheses.So your example is effectively the same as:
or
I would like to point out one more thing, the code above is wildly inefficient. Especially on large unsorted lists.
Here is a more sensible approach:
This is basically the same algorithm used for the Pure Perl version of
minin List::Util.You really should just be using
minfrom List::Util.