in reply to Can Schwartzian transform be modified to sort an arrayref uniquely?
You don't actually need to use a Schwartzian Transform here, because the operation that is "cached" by the transform (access to the 'num' field of the hash) isn't an expensive operation.
You were on the right track with the "%saw" stuff -- you can just prepend (syntactically; postpend operationally <laugh>) a sort:
my %saw; my @uniq = sort { $a->{num} cmp $b->{num} } grep { ! $saw{$_->{num}}++ } @{$data};
That's all you need.
And for a situation where you do need to use something like a Schwarzian transform, and you'd like to tighten up the code, you could add the grep to the front (or better, the back -- again, syntactically) of the map/sort/map. (Sometimes it's a little mind-bending, but as you've seen, you can string together the list-manipulation operators to your heart's content.) You can't get rid of the grep entirely because neither map nor sort will "drop" items from a list, only transform or rearrange them.
|
|---|