nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a mental block and hoping someone can help me out. I have the following data structure (an array of anonymous hashes with a named hash inside each anonymous hash):

[ { '221479714642518' => { 'name' => 'Family Mobile Phone and Ta +blet Repair', 'link' => 'https://www.facebook.com/F +amilyMobileRepairs/' } }, { '1626748754240747' => { 'name' => 'Bows and Baubles by Morga +n', 'link' => 'https://www.facebook.com/ +bowsandbaublesbymorgan/' } } ];

I want to sort the array based on the name key inside the inner hash. I'm trying something like this:

my @sorted_places = sort { $places->$a->{name} cmp $places->$b->{name} + } map { keys %$_ } @$places;

I get an error:

Can't call method "221479714642518" on unblessed reference

I've tried several different things but no luck. Any help is appreciated.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Sorting an array of anonymous hashes with named hashes using hash value
by AnomalousMonk (Archbishop) on May 07, 2016 at 00:04 UTC
    $places->$a->{name}

    That's an object method reference invocation on an object reference. Try
        $places->{$a}->{name}
    | No, that won't do it; try this.   (More rubber-duckery needed!)


    Give a man a fish:  <%-{-{-{-<

Re: Sorting an array of anonymous hashes with named hashes using hash value
by Anonymous Monk on May 07, 2016 at 00:04 UTC
    $a is an item from $places and $b is an item from $places, in sort you start with $a and $b not $places
      my @sorted_places = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ ( values %$_ )[0]->{name}, $_ ] } @$places;

        Much trickier than I thought. Thanks!

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks