#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11112008 use warnings; my $aref = [ { 'cname' => 'Smart Parking', 'balance' => 10.12, }, { 'cname' => 'Smart Parking', 'balance' => 10.22, }, { 'cname' => 'Smart Parking', 'balance' => 10.32, }, { 'cname' => 'Highview Parking', 'balance' => 20.12, }, { 'cname' => 'Highview Parking', 'balance' => 20.22, }, { 'cname' => 'Highview Parking', 'balance' => 20.32, }, { 'cname' => 'Highview Parking', 'balance' => 20.42, }, { 'cname' => 'ParkingEye', 'balance' => 30.12, }, { 'cname' => 'ParkingEye', 'balance' => 30.22, }, ]; my $temphash; for ( @$aref ) { $temphash->{$_->{cname}}{cname} = $_->{cname}; $temphash->{$_->{cname}}{balance} += $_->{balance}; $temphash->{$_->{cname}}{total}++; } my $answer = [ values %$temphash ]; use Data::Dump 'dd'; dd $answer;
Outputs:
[ { balance => 60.34, cname => "ParkingEye", total => 2 }, { balance => 81.08, cname => "Highview Parking", total => 4 }, { balance => 30.66, cname => "Smart Parking", total => 3 }, ]
In reply to Re: How to combine the values from hash using 'cname', along with total and balance.
by tybalt89
in thread How to combine the values from hash using 'cname', along with total and balance.
by Sami_R
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |