The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

List::Categorize - Categorize list items into a hash of named sublists

VERSION

This documentation describes List::Categorize version 0.01.

SYNOPSIS

    use List::Categorize qw(categorize);

    my %odds_and_evens = categorize { $_ % 2 ? 'ODD' : 'EVEN' } (1..9);

    # %odds_and_evens now contains
    # ( ODD => [ 1, 3, 5, 7, 9 ], EVEN => [ 2, 4, 6, 8 ] )

    my %capitalized = categorize {

        # Transform the element before placing it in the hash.
        $_ = ucfirst $_;

        # Use the first letter of the element as the category.
        substr($_, 0, 1);

    } qw( apple banana antelope bear canteloupe coyote );

    # %capitalized now contains
    # (
    #   A => [ 'Apple', 'Antelope' ],
    #   B => [ 'Banana', 'Bear' ],
    #   C => [ 'Canteloupe', 'Coyote' ]
    # )

DESCRIPTION

A simple module that creates a hash by applying a specified rule to each element of a provided list.

EXPORT

Nothing by default.

SUBROUTINES

categorize BLOCK LIST

    my %hash = categorize { $_ > 10 ? 'Big' : 'Little' } @list;

categorize creates a hash by running BLOCK for each element in LIST. The block returns a hash key (the "category") for the current element. (If it returns undef for a list element, that element is not placed in the resulting hash.)

The resulting hash contains a key for each category, and each key refers to a list of the elements that correspond to that category.

Within the block, $_ refers to the current list element. Elements can be modified before they're placed in the target hash by modifying the $_ variable:

    my %hash = categorize { $_ = uc $_; 'List' } qw( one two three );

    # %hash now contains ( List => [ 'ONE', 'TWO', 'THREE' ] )

NOTE: The categorizer should return a string, or undef. Other values are reserved for future use, and may cause unpredictable results in the current version.

SEE ALSO

List::Part

AUTHOR

Bill Odom, <wnodom at cpan.org>

BUGS

None known.

Please report any bugs or feature requests to bug-list-categorize at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Categorize.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc List::Categorize

You can also look for information at:

COPYRIGHT & LICENSE

Copyright (c) 2009 Bill Odom.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses at http://www.perlfoundation.org/artistic_license_1_0, and http://www.gnu.org/licenses/gpl-2.0.html.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.