develooper Front page | perl.perl6.users | Postings from June 2018

Re: need sort help

Thread Previous | Thread Next
Timo Paulssen
June 9, 2018 15:44
Re: need sort help
Message ID:
The magic trick is that the last line in the code block is the return value.

when you pass a function that takes a single argument (in this case just
the $_) to sort, it will call your function for every element in the
input array and use the result of that function to sort the elements by.

The "// 0" is there in every field because if the input line doesn't
have the expected format, you'll get a bunch of warnings, because the
variables will contain undefined values, you can try it for yourself
with inputs like "anaconda", "beetle", "zebra".

The last part of the puzzle is how sort will sort lists of lists
(because it has now replaced every string with a list of the parts we've
supplied); By default, sort uses the earliest entry in the list that is
different, so it's very much like lexicographic sort.

Does that explain everything?
  - Timo

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About