[Chicago-talk] Combine like hash elements

Andy Lester andy at petdance.com
Mon Nov 23 08:38:42 PST 2015


> {
> '11' => {'AMOUNT' => '20.00','ID' => '11','NAME' => 'Lincoln Park'},
> '12' => {'AMOUNT' => '38.00','ID' => '12','NAME' => 'Bucktown'},
> '13' => {'AMOUNT' => '41.00','ID' => '12','NAME' => 'Lincoln Park'}
> }
> 
> Can anyone tell me how I can combine them by 'NAME' so that I would have:
> 
> {
> '11' => {'AMOUNT' => '61.00','ID' => '11','NAME' => 'Lincoln Park'},
> '12' => {'AMOUNT' => '38.00','ID' => '12','NAME' => 'Bucktown'},
> }


> On Nov 23, 2015, at 10:36 AM, richard at rushlogistics.com wrote:
> 
> Did not include it because it did not come close but if you must see it here it is:
>  
>  %$w_ref = sort { %$w_ref{$a} <=> %$w_ref{$b} } keys %$w_ref;

What that’s doing is sorting.  There’s no combining going on.  You’re going to need to write something that goes through your hash values and finds the ones that have the same name and add the AMOUNTs together.


--
Andy Lester => www.petdance.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20151123/dff09d47/attachment.html>


More information about the Chicago-talk mailing list