O' Wise ones, Can an enlightened soul tell me if the Schwartzian transform can be modified to sort uniquely items inside an arrayref while running under 'use strict'?

My code looks like this (I'm trying to sort uniquely on the field called num):

#!/usr/local/bin/perl -w use strict; my $data = ( [ { num => 'OF1234', title => 'title OF1234', }, { num => 'AF1234', title => 'title AF1234', }, { num => 'AF1234', title => 'title AF1234', }, ] ); #print @{$data}; my @sorted = # now get the original array ref back map { $_->[0] } # use field 1 of anonymous array to sort (in this # case num) sort { $a->[1] cmp $b->[1] } # make an anonymous array of itself and num map { [$_, $_->{num}] } # start with the array ref and make it an array @{$data}; print join ', ', map { $_->{num} } @sorted; print "\n"; my %saw; undef %saw; my @unique = grep !$saw{$_->{num}}++, @sorted; print join ', ', map { $_->{num} } @unique; print "\n"; exit;
which produces the desired output (first line sorted, second line unique):
AF1234, AF1234, OF1234 AF1234, OF1234
Is there a way to get rid of the unsightly grep by using something similar to:
my @keys = sort keys %{ { map { $_ => 1 } @array1 } };
keeping in mind that we're talking about hash data inside the arrrayref.

I can't get my hands around it. It's a wonder that I understand the Schwartzian Transform to begin with. Here's a detailed explanation of the Schwartzian at http://www.5sigma.com/perl/schwtr.html for people wanting to know more. Thanks in advance,


In reply to Can Schwartzian transform be modified to sort an arrayref uniquely? by corporate_gadfly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.