Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to combine the values from hash using 'cname', along with total and balance.

by tybalt89 (Monsignor)
on Jan 29, 2020 at 18:37 UTC ( [id://11112037]=note: print w/replies, xml ) Need Help??


in reply to How to combine the values from hash using 'cname', along with total and balance.

#!/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 }, ]

Replies are listed 'Best First'.
Re^2: How to combine the values from hash using 'cname', along with total and balance.
by Sami_R (Sexton) on Jan 30, 2020 at 10:35 UTC

    Thank you so much

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found