develooper Front page | perl.perl6.users | Postings from April 2018

Re: Using HashBags

Thread Previous | Thread Next
From:
Siavash
Date:
April 7, 2018 12:16
Subject:
Re: Using HashBags
Message ID:
87po3b5hy2.fsf@gmail.com

Hi,

Don't know if there is a better way, but assuming you don't have control
over the data, you can do this:

my Bag $docents = @rows.map(*.pairup).Bag;

On 2018-04-07 10:10:52 GMT, mimosinnet wrote:
> Hi all,
>
> I do not seem to be able to get this done with the Bag or BagHash
> type:
>
> ---
> dd @rows;
>
>> Output: Array @rows = [["JF", 1], ["JF", 2], ["MM", 2], ["MPu", 2],
>> ["MM", 2], ["FE", 2], ["FV", 2], ["MPu", 2], ["JP", 2], ["JP", 2],
>> ["FV", 2], ["FV", 2], ["JF", 2], ["MM", 2], ["MPu", 2], ["MM", 2],
>> ["FE", 2], ["FV", 2], ["MPu", 2], ["JP", 2], ["JP", 2], ["JF", 4]]
>
> my %docents;
> for @rows -> @row {
> 	%docents{ @row[0] } += @row[1];
> }
>
> dd %docents;
>
>> Output: Hash %docents = {:FE(4), :FV(8), :JF(9), :JP(8), :MM(8),
>> :MPu(8)}
>
> ---
>
> As I understand it, this would better be achieved with the Bag or
> BagHash type. What would be the easy way?
>
> Thanks! 

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About