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 4257042
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:26:15+00:00 2026-05-21T05:26:15+00:00

I have an array of hash references. The hashes contain 2 keys, USER and

  • 0

I have an array of hash references. The hashes contain 2 keys, USER and PAGES. The goal here is to go through the array of hash references and keep a running total of the pages that the user printed on a printer (this comes from the event logs). I pulled the data from an Excel spreadsheet and used regexes to pull the username and pages. There are 182 rows in the spreadsheet and each row contains a username and the number of pages they printed on that job. Currently the script can print each print job (all 182) with the username and the pages they printed but I want to consolidate this down so it will show: username 266 (i.e. just show the username once, and the total number of pages they printed for the whole spreadsheet.

Here is my attempt at going through the array of hash references, seeing if the user already exists and if so, += the number of pages for that user into a new array of hash references (a smaller one). If not, then add the user to the new hash ref array:

my $criteria = "USER";
my @sorted_users = sort { $a->{$criteria} cmp $b->{$criteria} } @user_array_of_hash_refs;

my @hash_ref_arr;
my $hash_ref = \@hash_ref_arr;

foreach my $index (@sorted_users)
{
    my %hash = (USER=>"",PAGES=>"");
    if(exists $index{$index->{USER}})
    {
        $hash{PAGES}+=$index->{PAGES};
    }
    else
    {
        $hash{USER}=$index->{USER};
        $hash{PAGES}=$index->{PAGES};
    }
    push(@hash_ref_arr,{%hash});
}

But it gives me an error:

Global symbol “%index” requires explicit package name at …

Maybe my logic isn’t the best on this. Should I use arrays instead? It seems as though a hash is the best thing here, given the nature of my data. I just don’t know how to go about slimming the array of hash refs down to just get a username and the total pages they printed (I know I seem redundant but I’m just trying to be clear). Thank you.

  • 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-21T05:26:15+00:00Added an answer on May 21, 2026 at 5:26 am

    As mkb mentioned, the error is in the following line:

    if(exists $index{$index->{USER}})
    

    However, after reading your code, your logic is faulty. Simply correcting the syntax error will not provide your desired results.

    I would recommend skipping the use of temporary hash within the loop. Just work with the a results hash directly.

    For example:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my @test_data = (
        { USER => "tom", PAGES => "5" },
        { USER => "mary", PAGES => "2" },
        { USER => "jane", PAGES => "3" },
        { USER => "tom", PAGES => "3" }
    );
    
    my $criteria = "USER";
    my @sorted_users = sort { $a->{$criteria} cmp $b->{$criteria} } @test_data;
    
    my %totals;
    
    for my $index (@sorted_users) {
        if (not exists $totals{$index->{USER}}) {
            # initialize total for this user
            $totals{$index->{USER}} = 0;
        }
    
        # add to user's running total
        $totals{$index->{USER}} += $index->{PAGES}
    }
    
    print "$_: $totals{$_}\n" for keys %totals;
    

    This produces the following output:

    $ ./test.pl
    jane: 3
    tom: 8
    mary: 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of hash references: my @price = ( { id =>
I have an array of hashes, all with the same set of keys, e.g.:
I have an array, and I want to make a hash so I can
I have array that element is hash a = [{:history_date=>15/07/10}, {:open_price=>7.90}] I want to
So I've got this hash here, and I have a loop. When the loop
I have to pass two references as arguments to a subroutine (buildRanges) as hash
I have a array of hash refs. The date field in a hash is
I have array of hashes [{a => 1, b => 2}, {a=> 3, b
I have an array of hashes. I need to collect all hashes that meet
I have an array of objects which uses a delimited string as the keys.

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.