<div dir="ltr">You could also push the aggregation to the DB server and have it do the sum by name.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 23, 2015 at 12:52 PM,  <span dir="ltr"><<a href="mailto:richard@rushlogistics.com" target="_blank">richard@rushlogistics.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div>
<p>Hello Everybody,</p>
<p> </p>
<p>Thank you for all the replies. The ID field did not need to be preserved. 
However, I was having difficulty conceptualizing the manipulation of the hash so
I decided to use DBI->bind_colums instead and iterate throught the values in
a way that I found at least a bit more clear. Still working on cleaning this up
but this is at least the direction that I am going in.</p>
<p> </p>
<p>Thanks again for the help.</p>
<p> </p>
<p>   my ($DID, $AMOUNT, $NAME,);<br>    $sth->bind_columns(\($DID,
$AMOUNT, $NAME));<br><br>my $lastN;<br>my $lastA;<br>my @CAMS;<br>my
$keepN;<br>my $keepA;<br><br>while ($sth->fetch) {<br> <br>if ($lastN) {
<br>    <br>    if ($NAME eq $lastN) {<br><br>    #print "Combine
$AMOUNT, $NAME\n";<br>    $keepA = $AMOUNT + $lastA;<br>    $keepN =
$NAME;<br>    <br>    } else {<br><br>    $keepA = $AMOUNT;<br>   
$keepN = $NAME;<br><br>    }<br><br>} <br><br># print "$DID, $AMOUNT,
$NAME\n";<br><br>$lastN = $NAME;<br>$lastA = $AMOUNT;<br><br>push(@CAMS, $keepA
. " " . $keepN);</p><div><div class="h5"><br><br>}<br><br><br>On Mon, 23 Nov 2015 11:28:40 -0600, Steven
Lembark <<a href="mailto:lembark@wrkhors.com" target="_blank">lembark@wrkhors.com</a>> wrote:</div></div><p></p><div><div class="h5">
<blockquote style="border-left:2px solid #000000;padding-right:0px;padding-left:5px;margin-left:5px;margin-right:0px">On Mon, 23 Nov 2015
10:10:41 -0600<br> <a href="mailto:richard@rushlogistics.com" target="_blank">richard@rushlogistics.com</a> wrote:<br><br> > I have a hash
reference with the following structure:<br> > <br> > {<br> > '11' =>
{'AMOUNT' => '20.00','ID' => '11','NAME' => 'Lincoln Park'},<br> >
'12' => {'AMOUNT' => '38.00','ID' => '12','NAME' => 'Bucktown'},<br>
> '13' => {'AMOUNT' => '41.00','ID' => '12','NAME' => 'Lincoln
Park'}<br> > }<br> > <br> > Can anyone tell me how I can combine them
by 'NAME' so that I would have:<br> > <br> > {<br> > '11' =>
{'AMOUNT' => '61.00','ID' => '11','NAME' => 'Lincoln Park'},<br> >
'12' => {'AMOUNT' => '38.00','ID' => '12','NAME' => 'Bucktown'},<br>
> }<br><br> You don't seem to care about the keys (loosing key "13"
wasn't<br> a problem). <br><br> Q: Are the ID values important or could they be
discarded?<br><br> If so then just make a new hash keyed by NAME and
accumulate<br> the AMOUNT values:<br><br> my %resultz = ();<br><br> for( values
%inputz )<br> {<br> my ( $name, $amt, $id ) = @{ $_ }{ qw( NAME AMOUNT ID )
};<br> my $val<br> = $resultz{ $name } ||= { ID => $id, NAME => $name
};<br><br> $val->{ AMOUNT } += $amt;<br> }<br><br> # at this point %resultz
is keyed by name with an id and amount<br> # for each entry.<br><br> %inputz =
();<br><br> while( my ( $name, $val ) = each %resultz )<br> {<br> $inputz{
$val->{ ID } } = $val;<br> }<br><br> # at this point the original inputs hash
is keyed by ID<br> # with values having a name, accumulated amount, and id.<br>
# multiple id's are collapsed into a single ID with the <br> # value of the
first record for that name.<br><br> -- <br> Steven Lembark 3646 Flora Pl<br>
Workhorse Computing St Louis, MO 63110<br> <a href="mailto:lembark@wrkhors.com" target="_blank">lembark@wrkhors.com</a> +1 888 359
3508<br> _______________________________________________<br> Chicago-talk
mailing list<br> <a href="mailto:Chicago-talk@pm.org" target="_blank">Chicago-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/chicago-talk" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br>
</blockquote>
<p><br><br></p>

</div></div></div>
<br>_______________________________________________<br>
Chicago-talk mailing list<br>
<a href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/chicago-talk" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br></blockquote></div><br></div>