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

  • SEARCH
  • Home
  • 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 908471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:43:24+00:00 2026-05-15T16:43:24+00:00

In perl , i have an array of hashes like 0 HASH(0x98335e0) ‘title’ =>

  • 0

In perl , i have an array of hashes
like

0  HASH(0x98335e0)
   'title' => 1177
   'author' => 'ABC'
   'quantity' => '-100'


1  HASH(0x832a9f0)
   'title' => 1177
   'author' => 'ABC'
   'quantity' => '100'

2  HASH(0x98335e0)
   'title' => 1127
   'author' => 'DEF'
   'quantity' => '5100'


3  HASH(0x832a9f0)
   'title' => 1277
   'author' => 'XYZ'
   'quantity' => '1030'

Now I need to accumulate the quantity where title and author are same.
In the above structure for hash with title = 1177 and author =’ABC’ quantity can be accumulated into one and the entire structure should looks like below

0  HASH(0x98335e0)
   'title' => 1177
   'author' => 'ABC'
   'quantity' => 0

1  HASH(0x98335e0)
   'title' => 1127
   'author' => 'DEF'
   'quantity' => '5100'

2  HASH(0x832a9f0)
   'title' => 1277
   'author' => 'XYZ'
   'quantity' => '1030'

What is the best way i can do this accumulation so that it is optimised? Number of array elements can be very large. I dont mind adding an extra key to the hash to aid the same , but i dont want n lookups . Kindly advise

  • 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-15T16:43:24+00:00Added an answer on May 15, 2026 at 4:43 pm
    my %sum;
    for (@a) {
      $sum{ $_->{author} }{ $_->{title} } += $_->{quantity};
    }
    
    my @accumulated;
    foreach my $author (keys %sum) {
      foreach my $title (keys %{ $sum{$author} }) {
        push @accumulated => { title    => $title,
                               author   => $author,
                               quantity => $sum{$author}{$title},
                             };
      }
    }
    

    Not sure whether map makes it look nicer:

    my @accumulated =
      map {
        my $author = $_;
        map { author   => $author,
              title    => $_,
              quantity => $sum{$author}{$_},
            },
          keys %{ $sum{$author} };
      }
      keys %sum;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an array and hash like these #!/usr/bin/perl use warnings; use strict;
I have a perl hash of hashes like the following: $VAR1 = { 'ID_1'
I have a Perl subroutine which returns an array of vaules, and I'd like
I am currently learning Perl. I have Perl hash that contains references to hashes
I have a perl array of to-do tasks that looks like this: @todos =
I have a perl hash that I am indexing like this: my %hash; $hash{'number'}{'even'}
I have a perl object (reference to array of references) like the below: my
I have an array in Perl I want to print with space delimiters between
I write perl classes, but I don't know how to have a array or
I have a simple script trying to learn about hashes in Perl. #!/usr/bin/perl my

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.