You have a general approach from NetWallah and a specific impelmentation of that approach from tybalt89.

One detail of the OP is that the order of the lots (I assume these are parking lot names) in the output array seems to reflect the order of groups of lot names in the input array. If this is an actual requirement and not just an artifact of the OPed example, here's an approach to achieving this ordering. (Update: See tybalt89's solution for a much cleaner approach.)

c:\@Work\Perl\monks>perl -le "use strict; use warnings; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; use Data::Dump qw(dd); ;; 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 %lot_and_order; my %merged; for my $hr (@$aref) { my $lot_name = $hr->{'cname'}; $lot_and_order{$lot_name} = keys %lot_and_order unless exists $lot_and_order{$lot_name}; $merged{$lot_name}{'balance'} += $hr->{'balance'}; $merged{$lot_name}{'total'}++; } dd 'merged', \%merged; dd 'lot and order', \%lot_and_order; ;; my @out = map { { 'cname' => $_, 'balance' => $merged{$_}{'balance'}, 'tot +al' => $merged{$_}{'total'} } } map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, $lot_and_order{$_} ] } keys %lot_and_order ; dd 'in lot order', \@out; ;; my $ar_expected = [ { 'cname' => 'Smart Parking', 'balance' => '30.66', 'total' => '3', }, { 'cname' => 'Highview Parking', 'balance' => '81.08', 'total' => '4', }, { 'cname' => 'ParkingEye', 'balance' => '60.34', 'total' => '2', }, ]; ;; is_deeply \@out, $ar_expected, 'merged in lot order'; ;; done_testing; exit; " ( "merged", { "Highview Parking" => { balance => "81.08", total => 4 }, ParkingEye => { balance => "60.34", total => 2 }, "Smart Parking" => { balance => "30.66", total => 3 }, }, ) ( "lot and order", { "Highview Parking" => 1, ParkingEye => 2, "Smart Parking" => 0 }, ) ( "in lot order", [ { balance => "30.66", cname => "Smart Parking", total => 3 }, { balance => "81.08", cname => "Highview Parking", total => 4 }, { balance => "60.34", cname => "ParkingEye", total => 2 }, ], ) ok 1 - merged in lot order 1..1 ok 2 - no warnings 1..2
Note that no attempt is made to detect (update: in the sense of data validation) an input lot name that is not contiguous with all others of the same name. (Update: This solution does not require contiguous input lot names. The order of the first appearance of a lot name in the input will be the order of its appearance in the output.)


Give a man a fish:  <%-{-{-{-<


In reply to Re: How to combine the values from hash using 'cname', along with total and balance. by AnomalousMonk
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.