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

Re: Using HashBags

Thread Previous | Thread Next
From:
Vittore Scolari
Date:
April 8, 2018 15:53
Subject:
Re: Using HashBags
Message ID:
CAPDtZwRdY1F5PVQYakugtX8qBb5L3UKzpKWG3UtYw9iQoEA-bA@mail.gmail.com
Wouldn't here be better to use the % sigil?

my %docents = bag @rows.map: -> @row {@row[0] xx @row[1]};


On Sat, Apr 7, 2018 at 1:02 PM, Fernando Santagata <
nando.santagata@gmail.com> wrote:

> Hi,
>
> I'm not sure that I've understood what you need.
> If you get that array of arrays from a another process @rows and you wish
> to convert it into a Bag you can do this:
>
> my @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 Bag $docents = bag @rows.map: -> @row {@row[0] xx @row[1]};
> dd $docents;
>
> # Bag $docents = ("JP"=>8,"FV"=>8,"FE"=>4,"MPu"=>8,"MM"=>8,"JF"=>9).Bag
>
> Otherwise you can start collecting those pairs into a Bag from the start,
> but we would need more details then.
>
> HTH
>
>
> On Sat, Apr 7, 2018 at 12:10 PM, mimosinnet <mimosinnet@gmail.com> 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!
>> --
>> (≧∇≦) Mimosinnet (Linux User: #463211)
>>
>
>
>
> --
> Fernando Santagata
>

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