Array::Set - Perform set operations on arrays
This document describes version 0.063 of Array::Set (from Perl distribution Array-Set), released on 2021-10-12.
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]
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.
eq
allow_refs
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.
All functions are not exported by default, but exportable.
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").
" 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.
ignore_case
ignore_blanks
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_diff
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.
Perform intersection (find elements common in all the sets). Accept optional hashref as the first argument for options. See set_diff for known options.
Please visit the project's homepage at https://metacpan.org/release/Array-Set.
Source repository is at https://github.com/perlancar/perl-Array-Set.
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.
uniq
singleton
perlancar <perlancar@cpan.org>
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.
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.
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.
To install Array::Set, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Array::Set
CPAN shell
perl -MCPAN -e shell install Array::Set
For more information on module installation, please visit the detailed CPAN module installation guide.