[Chicago-talk] Combine like hash elements

Joel Berger joel.a.berger at gmail.com
Mon Nov 23 08:54:59 PST 2015


A quick attempt (without preserving the ids which are ambiguous as imran
said):

my %totals;
$totals{$_->{NAME}} += $_->{AMOUNT} for values %$orig;

On Mon, Nov 23, 2015 at 10:46 AM imran javaid <imranjj at gmail.com> wrote:

> I am guessing there is a typo in the sample input data. In the third
> entry, isn't the key and ID value supposed to be the same? Is it supposed
> to be 11 or 13? If it is the way it is or if it really is 13, then you need
> to arbitrate which key and/or ID the resulting hash will have.
>
> {
> '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'}
> }
>
>
> On Mon, Nov 23, 2015 at 10:38 AM, Andy Lester <andy at petdance.com> wrote:
>
>> {
>>
>>> '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
>>
>>
>> _______________________________________________
>> Chicago-talk mailing list
>> Chicago-talk at pm.org
>> http://mail.pm.org/mailman/listinfo/chicago-talk
>>
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20151123/9cd27283/attachment.html>


More information about the Chicago-talk mailing list