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

Re: need sort help

Thread Previous | Thread Next
From:
Timo Paulssen
Date:
June 9, 2018 15:44
Subject:
Re: need sort help
Message ID:
33d96093-e31b-0c33-7ef0-ff7b25fa76db@wakelift.de
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About