Schwartzian transform

Mark Overmeer mark at overmeer.net
Wed Aug 13 12:13:42 BST 2014


* Dermot (paikkos at gmail.com) [140813 11:08]:
> >
> >
> >     # Warning: map in void context used for side-effect of setting
> > {position}...
> >     # (I feel so dirty! ;-)
> >     my $count = 1;
> >     map  { $_->[1]{position} = $count++ }
> >     sort { $a->[0] <=> $b->[0] }
> >     map  { [$_->{position}, $_] }
> >         values %{$ref};

> That is filthy....but i like it. There are times I need to keep the
> structure as a hashref so I can find items by their key and there are times
> when I want to pass the data around as an ordered array so this works well
> for me too.

Sort is not lazy (cannot be lazy), so this suffices:

    $_->{position} = $count++
        for sort {$a->{position} <=> $b->{position}}
               values %$ref;
-- 
Regards,

               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net



More information about the london.pm mailing list