The stupid question is the question not asked PerlMonks

### Re^2: Sorting dates with the Schwartzian Transform

by GotToBTru (Parson)
 on Aug 03, 2011 at 17:16 UTC ( #918335=note: print w/ replies, xml ) Need Help??

Schwartzian, no doubt, but not enough transformation. The dates are originally in DD-MM-YYYY format and must be rearranged for effective sorting.

```@dates = map { \$_->[0] }
sort {  \$a->[1] >= \$b->[1] }
map { my \$f = \$_; /(\d\d)-(\d\d)-(\d{4})/g; [ \$f, "\$3\$2\$1" ] }
@dates;

Also, incomplete. There are 3 other columns that should figure in the sort. I'd like to see the OP's code.

On a side note, I had never heard of the ST before. I looked briefly in perlsyn to see how I would know that the map {} sort {} map {} would be executed in reverse order, but I didn't find it. Any clues?

Replies are listed 'Best First'.
Re^3: Sorting dates with the Schwartzian Transform
by johngg (Abbot) on Aug 03, 2011 at 22:42 UTC
... how I would know that the map {} sort {} map {} would be executed in reverse order, but I didn't find it. Any clues?

Both map and sort take a list (which is on the RHS), do some transformation of it then return the transformed list (to the LHS).

```my @mapped = map { # some transform code } @unmapped;
my @sorted = sort { # sorting code } @unsorted;

The ST code is just an extension of this right-to-left pattern:

• the first map extracts the sorted dates in the original DD-MM-YYYY format and assigns to the @dates array on the LHS of the assignment operator (=);

• but it can't do that before the sort has evaluated, sorting the items;

• which in turn can't do any sorting before the bottom (or rightmost) map has transformed some dates into something sort can work with, taking its raw material from the rightmost part of the expression which is the original, unsorted @dates array.

I hope this makes things a bit clearer.

Cheers,

JohnGG

Re^3: Sorting dates with the Schwartzian Transform
by Anonymous Monk on Aug 04, 2011 at 11:29 UTC
From: Schwartzian Transform
``` 19: #Knowing the context of certain operations
20: # and being able to chain them together
21: # is crucial to a deep and idiomatic
22: # understanding of Perl.

True dat.

Thanks to those who took the time. Some nice pointers to resources I was unaware of.

Except for the pointer to Google search. That was uncool ;).

Create A New User
Node Status?
node history
Node Type: note [id://918335]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
As of 2016-07-01 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (17 votes). Check out past polls.