Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 895331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:32:29+00:00 2026-05-15T14:32:29+00:00

After executing these lines in Perl: my $data = `curl ‘$url’`; my $pets =

  • 0

After executing these lines in Perl:

my $data = `curl '$url'`;
my $pets = XMLin($data)->(pets);

I have an array reference that contains references to hashes:

$VAR1 = [
      {
        'title' => 'cat',
        'count' => '210'
      },
      {
        'title' => 'dog',
        'count' => '210'
      }
]

In Perl, how do I sort the hashes first by count and secondarily by title. Then print to STDOUT the count followed by the title on each newline.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:32:29+00:00Added an answer on May 15, 2026 at 2:32 pm

    Assuming you want counts in descending order and titles ascending:

    print map join(" ", @$_{qw/ count title /}) . "\n",
          sort { $b->{count} <=> $a->{count}
                             ||
                 $a->{title} cmp $b->{title} }
          @$pets;
    

    That’s compact code written in a functional style. To help understand it, let’s look at equivalent code in a more familiar, imperative style.

    Perl’s sort operator takes an optional SUBNAME parameter that allows you to factor out your comparison and give it a name that describes what it does. When I do this, I like to begin the sub’s name with by_ to make sort by_... ready more naturally.

    To start, you might have written

    sub by_count_then_title {
      $b->{count} <=> $a->{count}
                  ||
      $a->{title} cmp $b->{title}
    }
    
    my @sorted = sort by_count_then_title @$pets;
    

    Note that no comma follows the SUBNAME in this form!

    To address another commenter’s question, you could use or rather than || in by_count_then_title if you find it more readable. Both <=> and cmp have higher precedence (which you might think of as binding more tightly) than || and or, so it’s strictly a matter of style.

    To print the sorted array, a more familiar choice might be

    foreach my $p (@sorted) {
      print "$p->{count} $p->{title}\n";
    }
    

    Perl uses $_ if you don’t specify the variable that gets each value, so the following has the same meaning:

    for (@sorted) {
      print "$_->{count} $_->{title}\n";
    }
    

    The for and foreach keywords are synonyms, but I find that the uses above, i.e., foreach if I’m going to name a variable or for otherwise, read most naturally.

    Using map, a close cousin of foreach, instead isn’t much different:

    map print("$_->{count} $_->{title}\n"), @sorted;
    

    You could also promote print through the map:

    print map "$_->{count} $_->{title}\n",
          @sorted;
    

    Finally, to avoid repetition of $_->{...}, the hash slice @$_{"count", "title"} gives us the values associated with count and title in the loop’s current record. Having the values, we need to join them with a single space and append a newline to the result, so

    print map join(" ", @$_{qw/ count title /}) . "\n",
          @sorted;
    

    Remember that qw// is shorthand for writing a list of strings. As this example shows, read a map expression back-to-front (or bottom-to-top the way I indented it): first sort the records, then format them, then print them.

    You could eliminate the temporary @sorted but call the named comparison:

    print map join(" ", @$_{qw/ count title /}) . "\n",
          sort by_count_then_title
          @$pets;
    

    If the application of join is just too verbose for your taste, then

    print map "@$_{qw/ count title /}\n",
          sort by_count_then_title
          @$pets;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.