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

Hi All,

I have an array of hashes like this:

====

'backupsize' => [ { 'totalsize' => '89', 'hostname' => 'aaaa', 'application' => 'IDB' }, { 'application' => 'IDB', 'hostname' => 'aaaa', 'totalsize' => '32770' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '14' }, { 'totalsize' => '7444', 'hostname' => 'cccc', 'application' => 'MSSQL' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '7' }, { 'application' => 'WinFS', 'totalsize' => '1830535', 'hostname' => 'aaaa' }, { 'hostname' => 'dddd', 'totalsize' => '10240', 'application' => 'Oracle8' }, { 'application' => 'Oracle8', 'totalsize' => '1318400', 'hostname' => 'dddd' }, { 'application' => 'Oracle8', 'totalsize' => '680704', 'hostname' => 'dddd' }, { 'application' => 'IDB', 'hostname' => 'aaaa', 'totalsize' => '256' }, { 'hostname' => 'aaaa', 'totalsize' => '348', 'application' => 'IDB' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '4990' }, { 'hostname' => 'aaaa', 'totalsize' => '229', 'application' => 'IDB' }, { 'application' => 'SAP', 'totalsize' => '6', 'hostname' => 'bbbb' }, { 'totalsize' => '944171', 'hostname' => 'bbbb', 'application' => 'SAP' }, { 'application' => 'IDB', 'hostname' => 'aaaa', 'totalsize' => '113' }, { 'totalsize' => '14544', 'hostname' => 'bbbb', 'application' => 'SAP' }, { 'application' => 'IDB', 'totalsize' => '29758', 'hostname' => 'aaaa' }, { 'application' => 'Oracle8', 'totalsize' => '10129', 'hostname' => 'dddd' }, { 'totalsize' => '16385', 'hostname' => 'aaaa', 'application' => 'IDB' }, { 'application' => 'SAP', 'totalsize' => '542', 'hostname' => 'bbbb' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '861957' }, { 'hostname' => 'aaaa', 'totalsize' => '231', 'application' => 'IDB' }, { 'application' => 'IDB', 'hostname' => 'aaaa', 'totalsize' => '260' }, { 'application' => 'Oracle8', 'hostname' => 'dddd', 'totalsize' => '4565' }, { 'hostname' => 'bbbb', 'totalsize' => '19534', 'application' => 'SAP' }, { 'hostname' => 'aaaa', 'totalsize' => '594', 'application' => 'IDB' }, { 'totalsize' => '1566381', 'hostname' => 'bbbb', 'application' => 'FileSystem' }, { 'application' => 'Oracle8', 'totalsize' => '256', 'hostname' => 'dddd' }, { 'totalsize' => '78', 'hostname' => 'bbbb', 'application' => 'SAP' }, { 'hostname' => 'aaaa', 'totalsize' => '104', 'application' => 'IDB' }, { 'totalsize' => '4620', 'hostname' => 'aaaa', 'application' => 'IDB' }, { 'hostname' => 'aaaa', 'totalsize' => '24011', 'application' => 'IDB' }, { 'hostname' => 'aaaa', 'totalsize' => '24694', 'application' => 'IDB' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '2054983' }, { 'totalsize' => '559', 'hostname' => 'bbbb', 'application' => 'SAP' } ]

====

I want to write perl code which would iterate through my array of hashes, and look for - if same hostname and application exists then it should add the total size.

My new array of hashes should contain only 1 entry for each hostname and application.

Could anyone please help?

Thanks in advance.

Replies are listed 'Best First'.
Re: Array of hashes
by NetWallah (Canon) on Aug 19, 2016 at 14:33 UTC
    use strict; use warnings; my ($outer_hash, $new_hash); $outer_hash->{backupsize} = [{ 'totalsize' => '89', 'hostname' => 'aaaa', 'application' => 'IDB' }, { 'application' => 'IDB', 'hostname' => 'aaaa', 'totalsize' => '32770' }, { 'application' => 'SAP', 'hostname' => 'bbbb', 'totalsize' => '14' }]; for my $entry(@{ $outer_hash->{backupsize} }){ next unless $entry->{hostname} and $entry->{application} and $entr +y->{totalsize}; $new_hash->{$entry->{hostname}} {$entry->{application}} += $entry- +>{totalsize}; } for my $h (sort keys %$new_hash){ print "HOST:$h:\n"; for my $app(keys %{ $new_hash->{$h}}){ print "\t",$new_hash->{$h}{$app},"\t$app\n"; } }
    OUTPUT:
    HOST:aaaa: 32859 IDB HOST:bbbb: 14 SAP

            "Software interprets lawyers as damage, and routes around them" - Larry Wall

      Thank you so much NetWallah :)
Re: Array of hashes
by stevieb (Canon) on Aug 19, 2016 at 13:17 UTC