my $coll=bag <1 2 2 3 2 4 4 2 2>;
say $coll; # => Bag(1, 2(5), 3, 4(2))
How to get the item (key) with the biggest value, i.e. the 2(5)
from this Bag collection?
(Optional) Is there a way to tell the Bag to count only those elements which are successive and disregard all which are not successive so that the result would be Bag(2(4), 4(2))
?
To answer your first question, there's a method for that:
say $coll.maxpairs; # 2 => 5
To answer your second question: no, you cannot. You can only devise some logic that will pre-process the values before getting to the Bag
.
<1 2 2 3 2 4 4 2 2>.map( *somelogic* ).Bag
Leaving the *somelogic* part as an exercise to the reader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With