NAME
List::Util::groupby - Group items of a list into several (possibly multilevel) buckets
VERSION
This document describes version 0.004 of List::Util::groupby (from Perl distribution List-Util-groupby), released on 2023-11-20.
SYNOPSIS
use List::Util::groupby qw(groupby hgroupby);
my @buckets = groupby { $_ % 2 } 1..10; # => [ [2,4,6,8,10], [1,3,5,7,9] ]
my @buckets = groupby { [$_ % 2, $_ % 3] } 1..10; # => [ [ [6], [4,10], [2,8]], [ [3,9], [1,7], [5] ] ]
my @recs = (
{name=>"andi",date=>"2023-09-29",result=>9.8},
{name=>"andi",date=>"2023-09-30",result=>10.3},
{name=>"budi",date=>"2023-09-29",result=>11.1},
{name=>"budi",date=>"2023-09-30",result=>10.5},
my %buckets = hgroupby { $_->{name} } @recs;
# => (
# andi => [ {name=>"andi",date=>"2023-09-29",result=>9.8} , {name=>"andi",date=>"2023-09-30",result=>10.3} ],
# budi => [ {name=>"budi",date=>"2023-09-29",result=>11.1}, {name=>"budi",date=>"2023-09-30",result=>10.5} ],
# )
my %buckets = hgroupby { [$_%2, $_%3] } 1..10;
#{
# "0" => { "0" => [6], "1" => [4, 10], "2" => [2, 8] },
# "1" => { "0" => [3, 9], "1" => [1, 7], "2" => [5] },
#}
DESCRIPTION
This module provides "groupby" and "hgroupby".
FUNCTIONS
Not exported by default but exportable.
groupby
Usage:
@buckets = groupby CODE ARRAY
Group a list into several buckets.
In CODE, $_
(as well as $_[0]
) is set to array element. $_[1]
is set to the index of the element, so you can still group elements by their position. Code is expected to return an integer index to indicate which bucket the item should be grouped into. Code can also return an arrayref of integer indices for multilevel buckets.
Return a (possibly multilevel) array of arrayrefs.
hgroupby
Usage:
%buckets = hgroupby CODE ARRAY
Just like "groupby", except code is expected to return keys (of arrayref of keys) and the function will return a (possibly multilevel) hash of arrayrefs.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/List-Util-groupby.
SOURCE
Source repository is at https://github.com/perlancar/perl-List-Util-groupby.
SEE ALSO
Array::Group, Array::GroupBy, List::GroupBy
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2023 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=List-Util-groupby
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.