john smith
john smith

Reputation: 33

How do I consolidate a hash in Perl?

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.

Upvotes: 3

Views: 286

Answers (4)

chuckx
chuckx

Reputation: 6877

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

Upvotes: 2

Dave Cross
Dave Cross

Reputation: 69244

my %totals;

$totals{$_->{USER}} += $_->{PAGES} for @user_array_of_hash_refs;

And then, to get the data out:

print "$_ : $totals{$_}\n" for keys %totals;

You could sort by usage too:

print "$_ : $totals{$_}\n" for sort { $totals{$a} <=> $totals{$b} } keys %totals;

Upvotes: 3

odrm
odrm

Reputation: 5249

my %total;
for my $name_pages_pair (@sorted_users) {
  $total{$name_pages_pair->{USER}} += $name_pages_pair->{PAGES};
}

for my $username (sort keys %total) {
  printf "%20s %6u\n", $username, $total{$username};
}

Upvotes: 0

Matt K
Matt K

Reputation: 13842

The error comes from this line:

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

The $ sigil in Perl 5 with {} after the name means that you are getting a scalar value out of a hash. There is no hash declared by the name %index. I think that you probably just need to add a -> operator so the problem line becomes:

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

but not having the data makes me unsure.

Also, good on you for using use strict or you would be instantiating the %index hash silently and wondering why your results didn't make any sense.

Upvotes: 0

Related Questions