<div dir="ltr"><div>I disagree that this should be put in a database unless you have sufficient values to warrant that. If you are dealing with just a few dozen values relying upon a database to essentially sort a hash is not very Perl-ish I think. At any rate, here is a more verbose example that uses Data::Dumper. The other examples provided before should be better, but I offer this because you can debug it and see what is happening line by line: <br><br>use strict;<br>use Data::Dumper;<br>my $targetRef;<br>my $hashRef = <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' => '11','NAME' => 'Lincoln Park'}<br>}; #ID on line 3 fixed to match first line id<br><br>foreach my $structId(keys %$hashRef)<br>{<br>    my $struct = $hashRef->{$structId};<br>    if (!$targetRef->{$struct->{'ID'}}){<br>    $targetRef->{$struct->{'ID'}} = {   'NAME'  => $struct->{'NAME'},<br>                                            'ID'    => $struct->{'ID'}, <br>                                            'AMOUNT'=> $struct->{'AMOUNT'} };<br>    } else {<br>    $targetRef->{$struct->{'ID'}}->{'AMOUNT'} +=<br>        $struct->{'AMOUNT'};<br>    }<br>}<br><br>print Dumper $targetRef;<br><br>1;<br><br></div>== produces ==<br><br>$VAR1 = {<br>          '12' => {<br>                    'ID' => '12',<br>                    'AMOUNT' => '38.00',<br>                    'NAME' => 'Bucktown'<br>                  },<br>          '11' => {<br>                    'NAME' => 'Lincoln Park',<br>                    'AMOUNT' => '61',<br>                    'ID' => '11'<br>                  }<br>        };<br><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 23, 2015 at 12:55 PM, imran javaid <span dir="ltr"><<a href="mailto:imranjj@gmail.com" target="_blank">imranjj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You could also push the aggregation to the DB server and have it do the sum by name.</div><div class="HOEnZb"><div class="h5"><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><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>
<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" target="_blank">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>
</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><br clear="all"><br>-- <br><div class="gmail_signature">Sincerely,<br><br><br>Joel Limardo</div>
</div></div>