Here is a potentially scalable approach using an in-memory SQLITE database:
use strict; use warnings; use Data::Dumper qw(Dumper); use DBI; 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 $dbh = DBI->connect("dbi:SQLite:dbname=:memory:"); $dbh->do("CREATE TABLE parking ( cname TEXT, balance REAL )"); for my $row (@$aref){ $dbh->do("INSERT INTO parking (cname,balance) VALUES ('$row->{cname +}',$row->{balance})"); } my $sth = $dbh->prepare("SELECT cname, sum(balance) as balance, count( +*) as count FROM parking GROUP BY cname ORDER BY cname"); $sth->execute; my $result ; while (my $r = $sth->fetchrow_hashref()) { push @$result, { balance => $r->{balance}, cname => $r->{cname}, t +otal => $r->{count} } } print Dumper $result;
It produces:
$VAR1 = [ { 'balance' => '81.08', 'total' => 4, 'cname' => 'Highview Parking' }, { 'balance' => '60.34', 'total' => 2, 'cname' => 'ParkingEye' }, { 'balance' => '30.66', 'total' => 3, 'cname' => 'Smart Parking' } ];
Plenty of room for making it more robust and improving performance .. left as an exercise.

                "From there to here, from here to there, funny things are everywhere." -- Dr. Seuss


In reply to Re: How to combine the values from hash using 'cname', along with total and balance. by NetWallah
in thread How to combine the values from hash using 'cname', along with total and balance. by Sami_R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.