NAME

Array::Set - Perform set operations on arrays

VERSION

This document describes version 0.063 of Array::Set (from Perl distribution Array-Set), released on 2021-10-12.

SYNOPSIS

 use Array::Set qw(set_diff set_symdiff set_union set_intersect);

 set_diff([1,2,3,4], [2,3,4,5]);            # => [1]
 set_diff([1,2,3,4], [2,3,4,5], [3,4,5,6]); # => [1]
 set_diff({ignore_case=>1}, ["a","b"], ["B","c"]);   # => ["a"]

 set_symdiff([1,2,3,4], [2,3,4,5]);            # => [1,5]
 set_symdiff([1,2,3,4], [2,3,4,5], [3,4,5,6]); # => [1,6]

 set_union([1,3,2,4], [2,3,4,5]);            # => [1,3,2,4,5]
 set_union([1,3,2,4], [2,3,4,5], [3,4,5,6]); # => [1,3,2,4,5,6]

 set_intersect([1,2,3,4], [2,3,4,5]);            # => [2,3,4]
 set_intersect([1,2,3,4], [2,3,4,5], [3,4,5,6]); # => [3,4]

DESCRIPTION

This module provides routines for performing set operations on arrays. Set is represented as a regular Perl array. All comparison is done with eq (string comparison) by default, but if your set contains references/undef, you can enable allow_refs option if you want to support references/undef. You have to make sure that the arrays do not contain duplicates; this module won't check that for you.

Characteristics and differences with other similar modules:

  • array-based

    Set is more appropriately implemented using Perl hash, but this module specifically wants to support interset operations on arrays.

    Underneath, it still uses hash (Tie::IxHash, to be exact) when performing the operations to do fast searching of values, at the expense of storage. See links to benchmarks in "SEE ALSO".

  • simple functional (non-OO) interface

  • interset operations accept more than two arguments

    For convenience and some performance benefits.

  • option to do case-insensitive comparison

  • option to ignore blanks

  • ordering preserved

    Which is the reason one usually uses array in the first place.

FUNCTIONS

All functions are not exported by default, but exportable.

set_diff([ \%opts ], \@set1, ...) => array

Perform difference (find elements in the first set not in the other sets). Accept optional hashref as the first argument for options. Known options:

  • ignore_case => bool (default: 0)

    If set to 1, will perform case-insensitive comparison.

  • ignore_blanks => bool (default: 0)

    If set to 1, will ignore blanks (" foo" == "foo" == "f o o").

  • allow_refs => bool (default: 0)

    If set to 1, will serialize references using Storable first to be able to perform the set operations on reference/undef values. Note that for reference values, ignore_case and ignore_blanks options are not meaningful and not observed.

set_symdiff([ \%opts ], \@set1, ...) => array

Perform symmetric difference (find elements in the first set not in the other set, as well as elements in the other set not in the first). Accept optional hashref as the first argument for options. See set_diff for known options.

set_union([ \%opts ], \@set1, ...) => array

Perform union (find elements in the first or in the other, duplicates removed). Accept optional hashref as the first argument for options. See set_diff for known options.

set_intersect([ \%opts ], \@set1, ...) => array

Perform intersection (find elements common in all the sets). Accept optional hashref as the first argument for options. See set_diff for known options.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Array-Set.

SOURCE

Source repository is at https://github.com/perlancar/perl-Array-Set.

SEE ALSO

See some benchmarks in Bencher::Scenarios::Array::Set.

App::setop to perform set operations on lines of files on the command-line.

Array::Utils, Set::Scalar, List::MoreUtils (uniq for union, singleton for symmetric diff), Set::Array, Array::AsObject, Set::Object, Set::Tiny.

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, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2016, 2015 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=Array-Set

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.